Skip to content

Commit 55b7a32

Browse files
author
Tatiana Selezneva
committed
Add tests for specs/template.go file
1 parent 57df67a commit 55b7a32

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

pkg/obs/specs/template_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package specs
18+
19+
import (
20+
"errors"
21+
"testing"
22+
23+
"github.com/stretchr/testify/require"
24+
25+
"k8s.io/release/pkg/obs/specs/specsfakes"
26+
)
27+
28+
var err = errors.New("error")
29+
30+
func TestBuildSpecs(t *testing.T) {
31+
testcases := []struct {
32+
createPkgDef func(t *testing.T) *PackageDefinition
33+
prepare func(mock *specsfakes.FakeImpl)
34+
verifyWrites func(*testing.T, *specsfakes.FakeImpl)
35+
shouldErr bool
36+
}{
37+
{ // happy path
38+
createPkgDef: func(t *testing.T) *PackageDefinition {
39+
return &PackageDefinition{
40+
Name: "kubeadm",
41+
SpecTemplatePath: "testdata/templates",
42+
SpecOutputPath: t.TempDir(),
43+
}
44+
},
45+
prepare: func(mock *specsfakes.FakeImpl) {},
46+
verifyWrites: func(t *testing.T, fi *specsfakes.FakeImpl) {},
47+
shouldErr: false,
48+
},
49+
{ // package definition is nil
50+
createPkgDef: func(t *testing.T) *PackageDefinition {
51+
return nil
52+
},
53+
prepare: func(mock *specsfakes.FakeImpl) {},
54+
verifyWrites: func(t *testing.T, fi *specsfakes.FakeImpl) {},
55+
shouldErr: true,
56+
},
57+
{ // spec template path doesn't exist
58+
createPkgDef: func(t *testing.T) *PackageDefinition {
59+
return &PackageDefinition{
60+
Name: "kubeadm",
61+
SpecTemplatePath: "does_not_exist",
62+
}
63+
},
64+
prepare: func(mock *specsfakes.FakeImpl) {
65+
mock.StatReturns(nil, err)
66+
},
67+
verifyWrites: func(t *testing.T, fi *specsfakes.FakeImpl) {},
68+
shouldErr: true,
69+
},
70+
{ // error during walking the template directory
71+
createPkgDef: func(t *testing.T) *PackageDefinition {
72+
return &PackageDefinition{
73+
Name: "kubeadm",
74+
SpecTemplatePath: "testdata/templates",
75+
SpecOutputPath: t.TempDir(),
76+
}
77+
},
78+
prepare: func(mock *specsfakes.FakeImpl) {
79+
mock.WalkReturns(err)
80+
},
81+
verifyWrites: func(t *testing.T, fi *specsfakes.FakeImpl) {},
82+
shouldErr: true,
83+
},
84+
}
85+
86+
for _, tc := range testcases {
87+
sut := &Specs{}
88+
89+
pkgDef := tc.createPkgDef(t)
90+
mock := &specsfakes.FakeImpl{}
91+
92+
tc.prepare(mock)
93+
sut.SetImpl(mock)
94+
95+
err := sut.BuildSpecs(pkgDef, false)
96+
if tc.shouldErr {
97+
require.Error(t, err)
98+
} else {
99+
require.NoError(t, err)
100+
tc.verifyWrites(t, mock)
101+
}
102+
}
103+
}

0 commit comments

Comments
 (0)