|
| 1 | +package k8s |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + sdk "github.com/openshift-online/ocm-sdk-go" |
| 7 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 8 | +) |
| 9 | + |
| 10 | +// TestNewWithConn tests the NewWithConn function which creates a Kubernetes client |
| 11 | +// using backplane with a provided OCM SDK connection. This allows connecting to clusters |
| 12 | +// in different OCM environments by providing a custom OCM connection. |
| 13 | +func TestNewWithConn(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + clusterID string |
| 17 | + options client.Options |
| 18 | + ocmConn *sdk.Connection |
| 19 | + wantErr bool |
| 20 | + }{ |
| 21 | + { |
| 22 | + // Test that passing a nil OCM connection returns an error |
| 23 | + name: "nil OCM connection", |
| 24 | + clusterID: "test-cluster-id", |
| 25 | + options: client.Options{}, |
| 26 | + ocmConn: nil, |
| 27 | + wantErr: true, |
| 28 | + }, |
| 29 | + { |
| 30 | + // Test that passing an empty cluster ID with nil connection returns an error |
| 31 | + name: "empty cluster ID with nil connection", |
| 32 | + clusterID: "", |
| 33 | + options: client.Options{}, |
| 34 | + ocmConn: nil, |
| 35 | + wantErr: true, |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + for _, tt := range tests { |
| 40 | + t.Run(tt.name, func(t *testing.T) { |
| 41 | + client, err := NewWithConn(tt.clusterID, tt.options, tt.ocmConn) |
| 42 | + if tt.wantErr { |
| 43 | + if err == nil { |
| 44 | + t.Errorf("NewWithConn() expected error but got none") |
| 45 | + } |
| 46 | + } else { |
| 47 | + if err != nil { |
| 48 | + t.Errorf("NewWithConn() unexpected error = %v", err) |
| 49 | + } |
| 50 | + if client == nil { |
| 51 | + t.Errorf("NewWithConn() returned nil client") |
| 52 | + } |
| 53 | + } |
| 54 | + }) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// TestNewAsBackplaneClusterAdminWithConn tests the NewAsBackplaneClusterAdminWithConn function |
| 59 | +// which creates an elevated Kubernetes client (backplane-cluster-admin) using a provided OCM |
| 60 | +// SDK connection. This function allows connecting to clusters in different OCM environments |
| 61 | +// with elevated permissions. |
| 62 | +func TestNewAsBackplaneClusterAdminWithConn(t *testing.T) { |
| 63 | + tests := []struct { |
| 64 | + name string |
| 65 | + clusterID string |
| 66 | + options client.Options |
| 67 | + ocmConn *sdk.Connection |
| 68 | + elevationReasons []string |
| 69 | + wantErr bool |
| 70 | + }{ |
| 71 | + { |
| 72 | + // Test that passing a nil OCM connection with elevation reasons returns an error |
| 73 | + name: "nil OCM connection", |
| 74 | + clusterID: "test-cluster-id", |
| 75 | + options: client.Options{}, |
| 76 | + ocmConn: nil, |
| 77 | + elevationReasons: []string{"testing"}, |
| 78 | + wantErr: true, |
| 79 | + }, |
| 80 | + { |
| 81 | + // Test that passing a nil OCM connection without elevation reasons also returns an error |
| 82 | + name: "nil OCM connection with no elevation reasons", |
| 83 | + clusterID: "test-cluster-id", |
| 84 | + options: client.Options{}, |
| 85 | + ocmConn: nil, |
| 86 | + elevationReasons: nil, |
| 87 | + wantErr: true, |
| 88 | + }, |
| 89 | + { |
| 90 | + // Test that passing an empty cluster ID with nil connection returns an error |
| 91 | + name: "empty cluster ID with nil connection", |
| 92 | + clusterID: "", |
| 93 | + options: client.Options{}, |
| 94 | + ocmConn: nil, |
| 95 | + elevationReasons: []string{"testing"}, |
| 96 | + wantErr: true, |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + for _, tt := range tests { |
| 101 | + t.Run(tt.name, func(t *testing.T) { |
| 102 | + client, err := NewAsBackplaneClusterAdminWithConn(tt.clusterID, tt.options, tt.ocmConn, tt.elevationReasons...) |
| 103 | + if tt.wantErr { |
| 104 | + if err == nil { |
| 105 | + t.Errorf("NewAsBackplaneClusterAdminWithConn() expected error but got none") |
| 106 | + } |
| 107 | + } else { |
| 108 | + if err != nil { |
| 109 | + t.Errorf("NewAsBackplaneClusterAdminWithConn() unexpected error = %v", err) |
| 110 | + } |
| 111 | + if client == nil { |
| 112 | + t.Errorf("NewAsBackplaneClusterAdminWithConn() returned nil client") |
| 113 | + } |
| 114 | + } |
| 115 | + }) |
| 116 | + } |
| 117 | +} |
0 commit comments