|
| 1 | +package shared_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/url" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/OctopusDeploy/cli/pkg/cmd/target/shared" |
| 9 | + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +// An AWS ECS target as the API returns it, trimmed to the fields the CLI reads. |
| 15 | +// Captured from /api/Spaces-1/machines on a server with an ECS target configured. |
| 16 | +const ecsTargetJson = `{ |
| 17 | + "Id": "Machines-1041", |
| 18 | + "Name": "aws ecs", |
| 19 | + "SpaceId": "Spaces-1", |
| 20 | + "EnvironmentIds": ["Environments-1"], |
| 21 | + "Roles": ["ecs"], |
| 22 | + "TenantIds": [], |
| 23 | + "TenantTags": [], |
| 24 | + "HealthStatus": "Healthy", |
| 25 | + "StatusSummary": "This machine was successfully health checked.", |
| 26 | + "IsDisabled": false, |
| 27 | + "Endpoint": { |
| 28 | + "CommunicationStyle": "AwsEcsCluster", |
| 29 | + "DefaultWorkerPoolId": "WorkerPools-1", |
| 30 | + "ClusterName": "repro-604-cluster", |
| 31 | + "Region": "ap-southeast-2", |
| 32 | + "AccountId": "", |
| 33 | + "UseInstanceRole": true, |
| 34 | + "AssumeRole": false, |
| 35 | + "AssumedRoleArn": null, |
| 36 | + "AssumedRoleSession": null, |
| 37 | + "AssumeRoleSessionDurationSeconds": null, |
| 38 | + "AssumeRoleExternalId": null, |
| 39 | + "Id": null, |
| 40 | + "LastModifiedOn": null, |
| 41 | + "LastModifiedBy": null, |
| 42 | + "Links": {} |
| 43 | + } |
| 44 | +}` |
| 45 | + |
| 46 | +// If this fails because the SDK learned to deserialise "AwsEcsCluster", the |
| 47 | +// unknown-type fallbacks below can start reporting real detail. |
| 48 | +func TestEcsTargetDeserialisesWithoutAnEndpoint(t *testing.T) { |
| 49 | + target := parseTarget(t, ecsTargetJson) |
| 50 | + |
| 51 | + assert.True(t, machines.IsNil(target.Endpoint), "expected the SDK to leave the endpoint nil for an AwsEcsCluster target") |
| 52 | +} |
| 53 | + |
| 54 | +func TestGetCommunicationStyle_EndpointMissing(t *testing.T) { |
| 55 | + target := parseTarget(t, ecsTargetJson) |
| 56 | + |
| 57 | + assert.NotPanics(t, func() { |
| 58 | + assert.Equal(t, "", shared.GetCommunicationStyle(target)) |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +func TestGetCommunicationStyle_NilTarget(t *testing.T) { |
| 63 | + assert.NotPanics(t, func() { |
| 64 | + assert.Equal(t, "", shared.GetCommunicationStyle(nil)) |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +func TestGetCommunicationStyle_KnownEndpoint(t *testing.T) { |
| 69 | + target := machines.NewDeploymentTarget("web-01", machines.NewListeningTentacleEndpoint(&url.URL{Scheme: "https", Host: "tentacle:10933"}, "thumbprint"), []string{"Environments-1"}, []string{"web"}) |
| 70 | + |
| 71 | + assert.Equal(t, "TentaclePassive", shared.GetCommunicationStyle(target)) |
| 72 | +} |
| 73 | + |
| 74 | +func TestGetEndpointDetails_EndpointMissing(t *testing.T) { |
| 75 | + target := parseTarget(t, ecsTargetJson) |
| 76 | + |
| 77 | + var details map[string]string |
| 78 | + assert.NotPanics(t, func() { |
| 79 | + details = shared.GetEndpointDetails(target) |
| 80 | + }) |
| 81 | + assert.Empty(t, details) |
| 82 | +} |
| 83 | + |
| 84 | +func TestGetEndpointDetails_KnownEndpoint(t *testing.T) { |
| 85 | + endpoint := machines.NewListeningTentacleEndpoint(&url.URL{Scheme: "https", Host: "tentacle:10933"}, "thumbprint") |
| 86 | + endpoint.TentacleVersionDetails = &machines.TentacleVersionDetails{Version: "6.3.1"} |
| 87 | + target := machines.NewDeploymentTarget("web-01", endpoint, []string{"Environments-1"}, []string{"web"}) |
| 88 | + |
| 89 | + details := shared.GetEndpointDetails(target) |
| 90 | + |
| 91 | + assert.Equal(t, "https://tentacle:10933", details["URI"]) |
| 92 | + assert.Equal(t, "6.3.1", details["Tentacle version"]) |
| 93 | +} |
| 94 | + |
| 95 | +func TestGetEndpointDetails_TentacleNeverHealthChecked(t *testing.T) { |
| 96 | + endpoint := machines.NewListeningTentacleEndpoint(&url.URL{Scheme: "https", Host: "tentacle:10933"}, "thumbprint") |
| 97 | + target := machines.NewDeploymentTarget("web-01", endpoint, []string{"Environments-1"}, []string{"web"}) |
| 98 | + |
| 99 | + var details map[string]string |
| 100 | + assert.NotPanics(t, func() { |
| 101 | + details = shared.GetEndpointDetails(target) |
| 102 | + }) |
| 103 | + assert.Equal(t, "Unknown", details["Tentacle version"]) |
| 104 | + assert.Equal(t, "https://tentacle:10933", details["URI"]) |
| 105 | +} |
| 106 | + |
| 107 | +func TestFormatUri_Missing(t *testing.T) { |
| 108 | + assert.NotPanics(t, func() { |
| 109 | + assert.Equal(t, "Unknown", shared.FormatUri(nil)) |
| 110 | + }) |
| 111 | +} |
| 112 | + |
| 113 | +func TestResolveDefaultWorkerPool_EndpointMissing(t *testing.T) { |
| 114 | + target := parseTarget(t, ecsTargetJson) |
| 115 | + |
| 116 | + assert.NotPanics(t, func() { |
| 117 | + assert.Equal(t, "N/A", shared.ResolveDefaultWorkerPool(target, map[string]string{}, "None")) |
| 118 | + }) |
| 119 | +} |
| 120 | + |
| 121 | +func parseTarget(t *testing.T, payload string) *machines.DeploymentTarget { |
| 122 | + t.Helper() |
| 123 | + |
| 124 | + target := &machines.DeploymentTarget{} |
| 125 | + require.NoError(t, json.Unmarshal([]byte(payload), target)) |
| 126 | + |
| 127 | + return target |
| 128 | +} |
0 commit comments