Skip to content

Commit

Permalink
unittests for MigrateV1beta2V1beta3
Browse files Browse the repository at this point in the history
Signed-off-by: Artiom Diomin <[email protected]>
  • Loading branch information
kron4eg committed Aug 12, 2024
1 parent ed4aeeb commit ea852ee
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 3 deletions.
4 changes: 3 additions & 1 deletion pkg/apis/kubeone/config/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ func MigrateV1beta2V1beta3(clusterFilePath string) ([]byte, error) {
{
path: yamled.Path{"addons"},
convertor: func(p yamled.Path) {
if originalManifest.Has(p) {
// we moved helmReleases inside the addons
if originalManifest.Has(p) || originalManifest.Has(yamled.Path{"helmReleases"}) {
ybuf, _ := kyaml.Marshal(newManifest.Addons)
addons, _ := yamled.Load(bytes.NewBuffer(ybuf))
originalManifest.Set(p, addons)
Expand All @@ -86,6 +87,7 @@ func MigrateV1beta2V1beta3(clusterFilePath string) ([]byte, error) {
{
path: yamled.Path{"addons"},
convertor: func(p yamled.Path) {
// cleanup addons from all the nil/zero/invalid values
originalManifest.Walk(p, func(key yamled.Path, value any) {
refval := reflect.ValueOf(value)

Expand Down
46 changes: 46 additions & 0 deletions pkg/apis/kubeone/config/migrate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright 2020 The KubeOne Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import (
"flag"
"testing"

"k8c.io/kubeone/pkg/testhelper"
)

var updateFlag = flag.Bool("update", false, "update testdata files")

func TestV1Beta2ToV1Beta3Migration(t *testing.T) {
tests := []string{
"simple",
"just addons",
"helm",
"addons and helm",
}

for _, test := range tests {
t.Run(test, func(t *testing.T) {
got, err := MigrateV1beta2V1beta3(testhelper.TestDataFSName(t, "_v1beta2.yaml"))
if err != nil {
t.Fatalf("%s", err)
}

testhelper.DiffOutput(t, testhelper.FSGoldenName(t), string(got), *updateFlag)
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: kubeone.k8c.io/v1beta3
kind: KubeOneCluster
versions:
kubernetes: 1.30.1
addons:
addons:
- addon:
name: name1
- helmRelease:
chart: kube-state-metrics
namespace: kube-state-metrics
releaseName: ksm
repoURL: https://prometheus-community.github.io/helm-charts
timeout: 0s
values:
- valuesFile: ksm-values.yaml
- inline:
replicas: 3
version: 4.22.3
path: something
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: kubeone.k8c.io/v1beta2
kind: KubeOneCluster

versions:
kubernetes: 1.30.1

addons:
path: "something"

addons:
- name: "name1"

helmReleases:
- releaseName: ksm
chart: kube-state-metrics
repoURL: https://prometheus-community.github.io/helm-charts
namespace: kube-state-metrics
version: 4.22.3
values:
- valuesFile: ksm-values.yaml
- inline:
replicas: 3
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: kubeone.k8c.io/v1beta3
kind: KubeOneCluster
versions:
kubernetes: 1.30.1
addons:
addons:
- helmRelease:
chart: kube-state-metrics
namespace: kube-state-metrics
releaseName: ksm
repoURL: https://prometheus-community.github.io/helm-charts
timeout: 0s
values:
- valuesFile: ksm-values.yaml
- inline:
replicas: 3
version: 4.22.3
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: kubeone.k8c.io/v1beta2
kind: KubeOneCluster

versions:
kubernetes: 1.30.1

helmReleases:
- releaseName: ksm
chart: kube-state-metrics
repoURL: https://prometheus-community.github.io/helm-charts
namespace: kube-state-metrics
version: 4.22.3
values:
- valuesFile: ksm-values.yaml
- inline:
replicas: 3
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: kubeone.k8c.io/v1beta3
kind: KubeOneCluster
versions:
kubernetes: 1.30.1
addons:
addons:
- addon:
name: name1
path: something
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: kubeone.k8c.io/v1beta2
kind: KubeOneCluster

versions:
kubernetes: 1.30.1

addons:
path: "something"

addons:
- name: "name1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: kubeone.k8c.io/v1beta3
kind: KubeOneCluster
versions:
kubernetes: 1.30.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: kubeone.k8c.io/v1beta2
kind: KubeOneCluster

versions:
kubernetes: 1.30.1
16 changes: 14 additions & 2 deletions pkg/testhelper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,22 @@ import (
"github.com/pmezard/go-difflib/difflib"
)

func fsName(t *testing.T, ext ...string) string {
t.Helper()

return strings.Join(append([]string{strings.ReplaceAll(t.Name(), "/", "-")}, ext...), "")
}

func FSGoldenName(t *testing.T) string {
t.Helper()

return strings.ReplaceAll(t.Name(), "/", "-") + ".golden"
return fsName(t, ".golden")
}

func TestDataFSName(t *testing.T, ext ...string) string {
t.Helper()

return filepath.Join([]string{"testdata", fsName(t, ext...)}...)
}

func DiffOutput(t *testing.T, name, output string, update bool) {
Expand All @@ -40,7 +52,7 @@ func DiffOutput(t *testing.T, name, output string, update bool) {
}

if update {
if errw := os.WriteFile(golden, []byte(output), 0600); errw != nil {
if errw := os.WriteFile(golden, []byte(output), 0o600); errw != nil {
t.Fatalf("failed to write updated fixture: %v", errw)
}
}
Expand Down

0 comments on commit ea852ee

Please sign in to comment.