|
| 1 | +/* |
| 2 | +Copyright 2021 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package networking |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/golang/mock/gomock" |
| 23 | + "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/trunks" |
| 24 | + . "github.com/onsi/gomega" |
| 25 | + |
| 26 | + infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1" |
| 27 | + "sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/networking/mock_networking" |
| 28 | +) |
| 29 | + |
| 30 | +func Test_GetOrCreateTrunk(t *testing.T) { |
| 31 | + mockCtrl := gomock.NewController(t) |
| 32 | + defer mockCtrl.Finish() |
| 33 | + |
| 34 | + tests := []struct { |
| 35 | + name string |
| 36 | + trunkName string |
| 37 | + portID string |
| 38 | + expect func(m *mock_networking.MockNetworkClientMockRecorder) |
| 39 | + // Note the 'wanted' port isn't so important, since it will be whatever we tell ListPort or CreatePort to return. |
| 40 | + // Mostly in this test suite, we're checking that ListPort/CreatePort is called with the expected port opts. |
| 41 | + want *trunks.Trunk |
| 42 | + wantErr bool |
| 43 | + }{ |
| 44 | + { |
| 45 | + "return trunk if found", |
| 46 | + "trunk-1", |
| 47 | + "port-1", |
| 48 | + func(m *mock_networking.MockNetworkClientMockRecorder) { |
| 49 | + m. |
| 50 | + ListTrunk(trunks.ListOpts{ |
| 51 | + Name: "trunk-1", |
| 52 | + PortID: "port-1", |
| 53 | + }).Return([]trunks.Trunk{{ |
| 54 | + Name: "trunk-1", |
| 55 | + ID: "port-1", |
| 56 | + }}, nil) |
| 57 | + }, |
| 58 | + &trunks.Trunk{Name: "trunk-1", ID: "port-1"}, |
| 59 | + false, |
| 60 | + }, |
| 61 | + { |
| 62 | + "creates trunk if not found", |
| 63 | + "trunk-1", |
| 64 | + "port-1", |
| 65 | + func(m *mock_networking.MockNetworkClientMockRecorder) { |
| 66 | + // No ports found |
| 67 | + m. |
| 68 | + ListTrunk(trunks.ListOpts{ |
| 69 | + Name: "trunk-1", |
| 70 | + PortID: "port-1", |
| 71 | + }).Return([]trunks.Trunk{}, nil) |
| 72 | + m. |
| 73 | + CreateTrunk(trunks.CreateOpts{ |
| 74 | + Name: "trunk-1", |
| 75 | + PortID: "port-1", |
| 76 | + Description: "Created by cluster-api-provider-openstack cluster test-cluster", |
| 77 | + }, |
| 78 | + ).Return(&trunks.Trunk{Name: "trunk-1", ID: "port-1"}, nil) |
| 79 | + }, |
| 80 | + &trunks.Trunk{Name: "trunk-1", ID: "port-1"}, |
| 81 | + false, |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + eventObject := &infrav1.OpenStackMachine{} |
| 86 | + for _, tt := range tests { |
| 87 | + t.Run(tt.name, func(t *testing.T) { |
| 88 | + g := NewWithT(t) |
| 89 | + mockClient := mock_networking.NewMockNetworkClient(mockCtrl) |
| 90 | + tt.expect(mockClient.EXPECT()) |
| 91 | + s := Service{ |
| 92 | + client: mockClient, |
| 93 | + } |
| 94 | + got, err := s.getOrCreateTrunk( |
| 95 | + eventObject, |
| 96 | + "test-cluster", |
| 97 | + tt.trunkName, |
| 98 | + tt.portID, |
| 99 | + ) |
| 100 | + if tt.wantErr { |
| 101 | + g.Expect(err).To(HaveOccurred()) |
| 102 | + } else { |
| 103 | + g.Expect(err).NotTo(HaveOccurred()) |
| 104 | + } |
| 105 | + g.Expect(got).To(Equal(tt.want)) |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
0 commit comments