diff --git a/pkg/upstream/write_test.go b/pkg/upstream/write_test.go index 4240acf478..062fd5b0dd 100644 --- a/pkg/upstream/write_test.go +++ b/pkg/upstream/write_test.go @@ -28,13 +28,41 @@ func Test_renderValuesYAMLForLicense(t *testing.T) { Data: map[string]string{"id": "cluster-id"}, }) + testUpstream := &types.Upstream{ + License: &kotsv1beta1.License{ + Spec: kotsv1beta1.LicenseSpec{ + LicenseID: "license-id", + AppSlug: "app-slug", + ChannelName: "channel-name", + Endpoint: "https://replicated.app", + Entitlements: map[string]kotsv1beta1.EntitlementField{ + "license-field": { + Title: "License Field", + Description: "This is a license field", + ValueType: "string", + Value: kotsv1beta1.EntitlementValue{ + Type: kotsv1beta1.String, + StrVal: "license-field-value", + }, + }, + }, + CustomerEmail: "customer@example.com", + CustomerName: "Customer Name", + LicenseType: "trial", + Signature: []byte{}, + }, + }, + ReplicatedRegistryDomain: "registry.replicated.com", + ReplicatedProxyDomain: "proxy.replicated.com", + } + type args struct { clientset kubernetes.Interface kotsStore store.Store unrenderedContents []byte u *types.Upstream replicatedSDKChartName string - isReplicatedSDK bool + isSubchart bool } tests := []struct { name string @@ -45,41 +73,16 @@ func Test_renderValuesYAMLForLicense(t *testing.T) { wantErr bool }{ { - name: "online install with sdk as a subchart and no existing replicated values", + name: "online install with sdk as a subchart and does not have existing replicated or global values", + airgap: false, args: args{ clientset: mockClientset, kotsStore: mockStore, unrenderedContents: []byte(`# Comment in values existing: value`), - u: &types.Upstream{ - License: &kotsv1beta1.License{ - Spec: kotsv1beta1.LicenseSpec{ - LicenseID: "license-id", - AppSlug: "app-slug", - ChannelName: "channel-name", - Endpoint: "https://replicated.app", - Entitlements: map[string]kotsv1beta1.EntitlementField{ - "license-field": { - Title: "License Field", - Description: "This is a license field", - ValueType: "string", - Value: kotsv1beta1.EntitlementValue{ - Type: kotsv1beta1.String, - StrVal: "license-field-value", - }, - }, - }, - CustomerEmail: "customer@example.com", - CustomerName: "Customer Name", - LicenseType: "trial", - Signature: []byte{}, - }, - }, - ReplicatedRegistryDomain: "registry.replicated.com", - ReplicatedProxyDomain: "proxy.replicated.com", - }, + u: testUpstream, replicatedSDKChartName: "replicated", - isReplicatedSDK: false, + isSubchart: true, }, mockStoreExpectations: func() { mockStore.EXPECT().GetAppIDFromSlug("app-slug").Return("app-id", nil) @@ -91,6 +94,326 @@ replicated: appID: app-id replicatedID: cluster-id userAgent: KOTS/v0.0.0-unknown +`), + }, + { + name: "online install with sdk as a subchart and has existing replicated and global values", + airgap: false, + args: args{ + clientset: mockClientset, + kotsStore: mockStore, + unrenderedContents: []byte(`# Comment in values +global: + test: value +existing: value +replicated: + test: value`), + u: testUpstream, + replicatedSDKChartName: "replicated", + isSubchart: true, + }, + mockStoreExpectations: func() { + mockStore.EXPECT().GetAppIDFromSlug("app-slug").Return("app-id", nil) + }, + want: []byte(`# Comment in values +global: + test: value +existing: value +replicated: + test: value + airgap: false + appID: app-id + replicatedID: cluster-id + userAgent: KOTS/v0.0.0-unknown +`), + }, + { + name: "airgap install with sdk as a subchart and does not have existing replicated or global values", + airgap: true, + args: args{ + clientset: mockClientset, + kotsStore: mockStore, + unrenderedContents: []byte(`# Comment in values +existing: value`), + u: testUpstream, + replicatedSDKChartName: "replicated", + isSubchart: true, + }, + mockStoreExpectations: func() { + mockStore.EXPECT().GetAppIDFromSlug("app-slug").Return("app-id", nil) + }, + want: []byte(`# Comment in values +existing: value +global: + replicated: + channelName: channel-name + customerEmail: customer@example.com + customerName: Customer Name + dockerconfigjson: eyJhdXRocyI6eyJwcm94eS5yZXBsaWNhdGVkLmNvbSI6eyJhdXRoIjoiYkdsalpXNXpaUzFwWkRwc2FXTmxibk5sTFdsayJ9LCJyZWdpc3RyeS5yZXBsaWNhdGVkLmNvbSI6eyJhdXRoIjoiYkdsalpXNXpaUzFwWkRwc2FXTmxibk5sTFdsayJ9fX0= + licenseFields: + license-field: + name: license-field + title: License Field + description: This is a license field + value: license-field-value + valueType: string + licenseID: license-id + licenseType: trial +replicated: + airgap: true + appID: app-id + license: | + metadata: + creationTimestamp: null + spec: + appSlug: app-slug + channelName: channel-name + customerEmail: customer@example.com + customerName: Customer Name + endpoint: https://replicated.app + entitlements: + license-field: + description: This is a license field + title: License Field + value: license-field-value + valueType: string + licenseID: license-id + licenseType: trial + signature: "" + status: {} + replicatedID: cluster-id + userAgent: KOTS/v0.0.0-unknown +`), + }, + { + name: "airgap install with sdk as a subchart and has existing replicated and global values", + airgap: true, + args: args{ + clientset: mockClientset, + kotsStore: mockStore, + unrenderedContents: []byte(`# Comment in values +global: + test: value +existing: value +replicated: + test: value`), + u: testUpstream, + replicatedSDKChartName: "replicated", + isSubchart: true, + }, + mockStoreExpectations: func() { + mockStore.EXPECT().GetAppIDFromSlug("app-slug").Return("app-id", nil) + }, + want: []byte(`# Comment in values +global: + test: value + replicated: + channelName: channel-name + customerEmail: customer@example.com + customerName: Customer Name + dockerconfigjson: eyJhdXRocyI6eyJwcm94eS5yZXBsaWNhdGVkLmNvbSI6eyJhdXRoIjoiYkdsalpXNXpaUzFwWkRwc2FXTmxibk5sTFdsayJ9LCJyZWdpc3RyeS5yZXBsaWNhdGVkLmNvbSI6eyJhdXRoIjoiYkdsalpXNXpaUzFwWkRwc2FXTmxibk5sTFdsayJ9fX0= + licenseFields: + license-field: + name: license-field + title: License Field + description: This is a license field + value: license-field-value + valueType: string + licenseID: license-id + licenseType: trial +existing: value +replicated: + test: value + airgap: true + appID: app-id + license: | + metadata: + creationTimestamp: null + spec: + appSlug: app-slug + channelName: channel-name + customerEmail: customer@example.com + customerName: Customer Name + endpoint: https://replicated.app + entitlements: + license-field: + description: This is a license field + title: License Field + value: license-field-value + valueType: string + licenseID: license-id + licenseType: trial + signature: "" + status: {} + replicatedID: cluster-id + userAgent: KOTS/v0.0.0-unknown +`), + }, + { + name: "online install with sdk as a parent chart and does not have existing global values", + airgap: false, + args: args{ + clientset: mockClientset, + kotsStore: mockStore, + unrenderedContents: []byte(`# Comment in values +existing: value`), + u: testUpstream, + replicatedSDKChartName: "replicated", + isSubchart: false, + }, + mockStoreExpectations: func() { + mockStore.EXPECT().GetAppIDFromSlug("app-slug").Return("app-id", nil) + }, + want: []byte(`# Comment in values +existing: value +airgap: false +appID: app-id +replicatedID: cluster-id +userAgent: KOTS/v0.0.0-unknown +`), + }, + { + name: "airgap install with sdk as a parent chart and does not have existing global values", + airgap: true, + args: args{ + clientset: mockClientset, + kotsStore: mockStore, + unrenderedContents: []byte(`# Comment in values +existing: value`), + u: testUpstream, + replicatedSDKChartName: "replicated", + isSubchart: false, + }, + mockStoreExpectations: func() { + mockStore.EXPECT().GetAppIDFromSlug("app-slug").Return("app-id", nil) + }, + want: []byte(`# Comment in values +existing: value +global: + replicated: + channelName: channel-name + customerEmail: customer@example.com + customerName: Customer Name + dockerconfigjson: eyJhdXRocyI6eyJwcm94eS5yZXBsaWNhdGVkLmNvbSI6eyJhdXRoIjoiYkdsalpXNXpaUzFwWkRwc2FXTmxibk5sTFdsayJ9LCJyZWdpc3RyeS5yZXBsaWNhdGVkLmNvbSI6eyJhdXRoIjoiYkdsalpXNXpaUzFwWkRwc2FXTmxibk5sTFdsayJ9fX0= + licenseFields: + license-field: + name: license-field + title: License Field + description: This is a license field + value: license-field-value + valueType: string + licenseID: license-id + licenseType: trial +airgap: true +appID: app-id +license: | + metadata: + creationTimestamp: null + spec: + appSlug: app-slug + channelName: channel-name + customerEmail: customer@example.com + customerName: Customer Name + endpoint: https://replicated.app + entitlements: + license-field: + description: This is a license field + title: License Field + value: license-field-value + valueType: string + licenseID: license-id + licenseType: trial + signature: "" + status: {} +replicatedID: cluster-id +userAgent: KOTS/v0.0.0-unknown +`), + }, + { + name: "online install with sdk as a parent chart and has existing global values", + airgap: false, + args: args{ + clientset: mockClientset, + kotsStore: mockStore, + unrenderedContents: []byte(`# Comment in values +global: + test: value +existing: value`), + u: testUpstream, + replicatedSDKChartName: "replicated", + isSubchart: false, + }, + mockStoreExpectations: func() { + mockStore.EXPECT().GetAppIDFromSlug("app-slug").Return("app-id", nil) + }, + want: []byte(`# Comment in values +global: + test: value +existing: value +airgap: false +appID: app-id +replicatedID: cluster-id +userAgent: KOTS/v0.0.0-unknown +`), + }, + { + name: "airgap install with sdk as a parent chart and does not have existing global values", + airgap: true, + args: args{ + clientset: mockClientset, + kotsStore: mockStore, + unrenderedContents: []byte(`# Comment in values +global: + test: value +existing: value`), + u: testUpstream, + replicatedSDKChartName: "replicated", + isSubchart: false, + }, + mockStoreExpectations: func() { + mockStore.EXPECT().GetAppIDFromSlug("app-slug").Return("app-id", nil) + }, + want: []byte(`# Comment in values +global: + test: value + replicated: + channelName: channel-name + customerEmail: customer@example.com + customerName: Customer Name + dockerconfigjson: eyJhdXRocyI6eyJwcm94eS5yZXBsaWNhdGVkLmNvbSI6eyJhdXRoIjoiYkdsalpXNXpaUzFwWkRwc2FXTmxibk5sTFdsayJ9LCJyZWdpc3RyeS5yZXBsaWNhdGVkLmNvbSI6eyJhdXRoIjoiYkdsalpXNXpaUzFwWkRwc2FXTmxibk5sTFdsayJ9fX0= + licenseFields: + license-field: + name: license-field + title: License Field + description: This is a license field + value: license-field-value + valueType: string + licenseID: license-id + licenseType: trial +existing: value +airgap: true +appID: app-id +license: | + metadata: + creationTimestamp: null + spec: + appSlug: app-slug + channelName: channel-name + customerEmail: customer@example.com + customerName: Customer Name + endpoint: https://replicated.app + entitlements: + license-field: + description: This is a license field + title: License Field + value: license-field-value + valueType: string + licenseID: license-id + licenseType: trial + signature: "" + status: {} +replicatedID: cluster-id +userAgent: KOTS/v0.0.0-unknown `), }, } @@ -100,7 +423,7 @@ replicated: t.Setenv("DISABLE_OUTBOUND_CONNECTIONS", "true") } tt.mockStoreExpectations() - got, err := renderValuesYAMLForLicense(tt.args.clientset, tt.args.kotsStore, tt.args.unrenderedContents, tt.args.u, tt.args.replicatedSDKChartName, tt.args.isReplicatedSDK) + got, err := renderValuesYAMLForLicense(tt.args.clientset, tt.args.kotsStore, tt.args.unrenderedContents, tt.args.u, tt.args.replicatedSDKChartName, tt.args.isSubchart) if (err != nil) != tt.wantErr { t.Errorf("renderValuesYAMLForLicense() error = %v, wantErr %v", err, tt.wantErr) return