|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/google/uuid" |
| 9 | + registry "github.com/stackitcloud/stackit-sdk-go/services/registry/v1api" |
| 10 | + |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + testProjectId = uuid.NewString() |
| 16 | + testArtifactoryId = uuid.NewString() |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + testRegion = "eu01" |
| 21 | + testArtifactoryName = "my-artifactory" |
| 22 | +) |
| 23 | + |
| 24 | +type registryClientMocked struct { |
| 25 | + getArtifactoryFails bool |
| 26 | + getArtifactoryResp *registry.Artifactory |
| 27 | +} |
| 28 | + |
| 29 | +func (m *registryClientMocked) newMock() registry.DefaultAPI { |
| 30 | + return registry.DefaultAPIServiceMock{ |
| 31 | + GetArtifactoryExecuteMock: utils.Ptr(func(_ registry.ApiGetArtifactoryRequest) (*registry.Artifactory, error) { |
| 32 | + if m.getArtifactoryFails { |
| 33 | + return nil, fmt.Errorf("could not get artifactory") |
| 34 | + } |
| 35 | + return m.getArtifactoryResp, nil |
| 36 | + }), |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestGetArtifactoryName(t *testing.T) { |
| 41 | + tests := []struct { |
| 42 | + description string |
| 43 | + getArtifactoryFails bool |
| 44 | + getArtifactoryResp *registry.Artifactory |
| 45 | + isValid bool |
| 46 | + expectedOutput string |
| 47 | + }{ |
| 48 | + { |
| 49 | + description: "base", |
| 50 | + getArtifactoryResp: ®istry.Artifactory{ |
| 51 | + Name: testArtifactoryName, |
| 52 | + }, |
| 53 | + isValid: true, |
| 54 | + expectedOutput: testArtifactoryName, |
| 55 | + }, |
| 56 | + { |
| 57 | + description: "get artifactory fails", |
| 58 | + getArtifactoryFails: true, |
| 59 | + isValid: false, |
| 60 | + }, |
| 61 | + { |
| 62 | + description: "empty response (nil)", |
| 63 | + getArtifactoryResp: nil, |
| 64 | + isValid: false, |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + for _, tt := range tests { |
| 69 | + t.Run(tt.description, func(t *testing.T) { |
| 70 | + client := ®istryClientMocked{ |
| 71 | + getArtifactoryFails: tt.getArtifactoryFails, |
| 72 | + getArtifactoryResp: tt.getArtifactoryResp, |
| 73 | + } |
| 74 | + |
| 75 | + output, err := GetArtifactoryName(context.Background(), client.newMock(), testProjectId, testRegion, testArtifactoryId) |
| 76 | + |
| 77 | + if tt.isValid && err != nil { |
| 78 | + t.Errorf("failed on valid input: %v", err) |
| 79 | + } |
| 80 | + if !tt.isValid && err == nil { |
| 81 | + t.Errorf("did not fail on invalid input") |
| 82 | + } |
| 83 | + if tt.isValid && output != tt.expectedOutput { |
| 84 | + t.Errorf("expected output %q, got %q", tt.expectedOutput, output) |
| 85 | + } |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
0 commit comments