|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/replicatedhq/embedded-cluster/pkg/release" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func Test_getLicenseFromFilepath(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + licenseContents string |
| 16 | + wantErr string |
| 17 | + useRelease bool |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "no license, no release", |
| 21 | + wantErr: "", |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "no license, with release", |
| 25 | + useRelease: true, |
| 26 | + wantErr: `no license was provided for embedded-cluster-smoke-test-staging-app and one is required, please rerun with '--license <path to license file>'`, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "valid license, no release", |
| 30 | + licenseContents: ` |
| 31 | +spec: |
| 32 | + appSlug: embedded-cluster-smoke-test-staging-app |
| 33 | + channelID: "2cHXb1RCttzpR0xvnNWyaZCgDBP" |
| 34 | + isEmbeddedClusterDownloadEnabled: true |
| 35 | + `, |
| 36 | + wantErr: "a license was provided but no release was found in binary, please rerun without the license flag", |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "valid license, with release", |
| 40 | + useRelease: true, |
| 41 | + licenseContents: ` |
| 42 | +spec: |
| 43 | + appSlug: embedded-cluster-smoke-test-staging-app |
| 44 | + channelID: "2cHXb1RCttzpR0xvnNWyaZCgDBP" |
| 45 | + isEmbeddedClusterDownloadEnabled: true |
| 46 | + `, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "valid multi-channel license, with release", |
| 50 | + useRelease: true, |
| 51 | + licenseContents: ` |
| 52 | +spec: |
| 53 | + appSlug: embedded-cluster-smoke-test-staging-app |
| 54 | + channelID: "OtherChannelID" |
| 55 | + isEmbeddedClusterDownloadEnabled: true |
| 56 | + channels: |
| 57 | + - channelID: OtherChannelID |
| 58 | + channelName: OtherChannel |
| 59 | + channelSlug: other-channel |
| 60 | + isDefault: true |
| 61 | + - channelID: 2cHXb1RCttzpR0xvnNWyaZCgDBP |
| 62 | + channelName: ExpectedChannel |
| 63 | + channelSlug: expected-channel |
| 64 | + isDefault: false |
| 65 | + `, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "expired license, with release", |
| 69 | + useRelease: true, |
| 70 | + licenseContents: ` |
| 71 | +spec: |
| 72 | + appSlug: embedded-cluster-smoke-test-staging-app |
| 73 | + channelID: "2cHXb1RCttzpR0xvnNWyaZCgDBP" |
| 74 | + isEmbeddedClusterDownloadEnabled: true |
| 75 | + entitlements: |
| 76 | + expires_at: |
| 77 | + description: License Expiration |
| 78 | + signature: {} |
| 79 | + title: Expiration |
| 80 | + value: "2024-06-03T00:00:00Z" |
| 81 | + valueType: String |
| 82 | + `, |
| 83 | + wantErr: "license expired on 2024-06-03 00:00:00 +0000 UTC, please provide a valid license", |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "license with no expiration, with release", |
| 87 | + useRelease: true, |
| 88 | + licenseContents: ` |
| 89 | +spec: |
| 90 | + appSlug: embedded-cluster-smoke-test-staging-app |
| 91 | + channelID: "2cHXb1RCttzpR0xvnNWyaZCgDBP" |
| 92 | + isEmbeddedClusterDownloadEnabled: true |
| 93 | + entitlements: |
| 94 | + expires_at: |
| 95 | + description: License Expiration |
| 96 | + signature: {} |
| 97 | + title: Expiration |
| 98 | + value: "" |
| 99 | + valueType: String |
| 100 | + `, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "license with 100 year expiration, with release", |
| 104 | + useRelease: true, |
| 105 | + licenseContents: ` |
| 106 | +spec: |
| 107 | + appSlug: embedded-cluster-smoke-test-staging-app |
| 108 | + channelID: "2cHXb1RCttzpR0xvnNWyaZCgDBP" |
| 109 | + isEmbeddedClusterDownloadEnabled: true |
| 110 | + entitlements: |
| 111 | + expires_at: |
| 112 | + description: License Expiration |
| 113 | + signature: {} |
| 114 | + title: Expiration |
| 115 | + value: "2124-06-03T00:00:00Z" |
| 116 | + valueType: String |
| 117 | + `, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "embedded cluster not enabled, with release", |
| 121 | + useRelease: true, |
| 122 | + licenseContents: ` |
| 123 | +spec: |
| 124 | + appSlug: embedded-cluster-smoke-test-staging-app |
| 125 | + channelID: "2cHXb1RCttzpR0xvnNWyaZCgDBP" |
| 126 | + isEmbeddedClusterDownloadEnabled: false |
| 127 | + `, |
| 128 | + wantErr: "license does not have embedded cluster enabled, please provide a valid license", |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "incorrect license (multichan license)", |
| 132 | + useRelease: true, |
| 133 | + licenseContents: ` |
| 134 | +spec: |
| 135 | + appSlug: embedded-cluster-smoke-test-staging-app |
| 136 | + channelID: "2i9fCbxTNIhuAOaC6MoKMVeGzuK" |
| 137 | + isEmbeddedClusterDownloadEnabled: false |
| 138 | + channels: |
| 139 | + - channelID: 2i9fCbxTNIhuAOaC6MoKMVeGzuK |
| 140 | + channelName: Stable |
| 141 | + channelSlug: stable |
| 142 | + isDefault: true |
| 143 | + - channelID: 4l9fCbxTNIhuAOaC6MoKMVeV3K |
| 144 | + channelName: Alternate |
| 145 | + channelSlug: alternate |
| 146 | + isDefault: false |
| 147 | + `, |
| 148 | + wantErr: "binary channel 2cHXb1RCttzpR0xvnNWyaZCgDBP (CI) not present in license, channels allowed by license are: stable (2i9fCbxTNIhuAOaC6MoKMVeGzuK), alternate (4l9fCbxTNIhuAOaC6MoKMVeV3K)", |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "incorrect license (pre-multichan license)", |
| 152 | + useRelease: true, |
| 153 | + licenseContents: ` |
| 154 | +spec: |
| 155 | + appSlug: embedded-cluster-smoke-test-staging-app |
| 156 | + channelID: "2i9fCbxTNIhuAOaC6MoKMVeGzuK" |
| 157 | + channelName: "Stable" |
| 158 | + isEmbeddedClusterDownloadEnabled: false |
| 159 | + `, |
| 160 | + wantErr: "binary channel 2cHXb1RCttzpR0xvnNWyaZCgDBP (CI) not present in license, channels allowed by license are: Stable (2i9fCbxTNIhuAOaC6MoKMVeGzuK)", |
| 161 | + }, |
| 162 | + } |
| 163 | + for _, tt := range tests { |
| 164 | + t.Run(tt.name, func(t *testing.T) { |
| 165 | + req := require.New(t) |
| 166 | + |
| 167 | + tmpdir, err := os.MkdirTemp("", "license") |
| 168 | + defer os.RemoveAll(tmpdir) |
| 169 | + req.NoError(err) |
| 170 | + |
| 171 | + licenseFile, err := os.Create(tmpdir + "/license.yaml") |
| 172 | + req.NoError(err) |
| 173 | + _, err = licenseFile.Write([]byte(tt.licenseContents)) |
| 174 | + req.NoError(err) |
| 175 | + |
| 176 | + dataMap := map[string][]byte{} |
| 177 | + if tt.useRelease { |
| 178 | + dataMap["release.yaml"] = []byte(` |
| 179 | +# channel release object |
| 180 | +channelID: "2cHXb1RCttzpR0xvnNWyaZCgDBP" |
| 181 | +channelSlug: "CI" |
| 182 | +appSlug: "embedded-cluster-smoke-test-staging-app" |
| 183 | +versionLabel: testversion |
| 184 | +`) |
| 185 | + } |
| 186 | + err = release.SetReleaseDataForTests(dataMap) |
| 187 | + req.NoError(err) |
| 188 | + |
| 189 | + if tt.licenseContents != "" { |
| 190 | + _, err = getLicenseFromFilepath(filepath.Join(tmpdir, "license.yaml")) |
| 191 | + } else { |
| 192 | + _, err = getLicenseFromFilepath("") |
| 193 | + } |
| 194 | + |
| 195 | + if tt.wantErr != "" { |
| 196 | + req.EqualError(err, tt.wantErr) |
| 197 | + } else { |
| 198 | + req.NoError(err) |
| 199 | + } |
| 200 | + }) |
| 201 | + } |
| 202 | +} |
0 commit comments