@@ -18,9 +18,9 @@ import (
18
18
"io/ioutil"
19
19
"math/rand"
20
20
"os"
21
+ "path"
21
22
"path/filepath"
22
23
"strconv"
23
- "strings"
24
24
"testing"
25
25
26
26
gen "github.com/operator-framework/operator-sdk/internal/generate/gen"
@@ -30,25 +30,18 @@ import (
30
30
)
31
31
32
32
const (
33
- testAPIVersion = "cache.example.com/v1alpha1"
34
- testKind = "Memcached"
33
+ testGroup = "cache.example.com"
34
+ testVersion = "v1alpha1"
35
+ testKind = "Memcached"
36
+ )
37
+
38
+ var (
39
+ testDataDir = filepath .Join (".." , "testdata" )
40
+ testGoDataDir = filepath .Join (testDataDir , "go" )
41
+ testAPIVersion = path .Join (testGroup , testVersion )
35
42
)
36
43
37
44
func TestGenerate (t * testing.T ) {
38
- tfDir := getTestFrameworkPath (t )
39
- // Must change directories since the test framework dir is a sub-module.
40
- wd , err := os .Getwd ()
41
- if err != nil {
42
- t .Fatal (err )
43
- }
44
- defer func () {
45
- if err = os .Chdir (wd ); err != nil {
46
- t .Fatal (err )
47
- }
48
- }()
49
- if err = os .Chdir (tfDir ); err != nil {
50
- t .Fatal (err )
51
- }
52
45
tmp , err := ioutil .TempDir ("" , "" )
53
46
if err != nil {
54
47
t .Fatal (err )
@@ -69,11 +62,21 @@ func TestGenerate(t *testing.T) {
69
62
}{
70
63
{
71
64
"Generate Go CRD" ,
72
- NewCRDGo (gen.Config {OutputDir : filepath .Join (tmp , strconv .Itoa (rand .Int ()))}),
65
+ NewCRDGo (gen.Config {
66
+ Inputs : map [string ]string {
67
+ APIsDirKey : filepath .Join (testGoDataDir , scaffold .ApisDir ),
68
+ },
69
+ OutputDir : filepath .Join (tmp , strconv .Itoa (rand .Int ())),
70
+ }),
73
71
},
74
72
{
75
73
"Generate non-Go CRD" ,
76
- NewCRDNonGo (gen.Config {OutputDir : filepath .Join (tmp , strconv .Itoa (rand .Int ()))}, * r ),
74
+ NewCRDNonGo (gen.Config {
75
+ Inputs : map [string ]string {
76
+ APIsDirKey : filepath .Join (testGoDataDir , scaffold .ApisDir ),
77
+ },
78
+ OutputDir : filepath .Join (tmp , strconv .Itoa (rand .Int ())),
79
+ }, * r ),
77
80
},
78
81
}
79
82
@@ -88,22 +91,11 @@ func TestGenerate(t *testing.T) {
88
91
}
89
92
90
93
func TestCRDGo (t * testing.T ) {
91
- tfDir := getTestFrameworkPath (t )
92
- // Must change directories since the test framework dir is a sub-module.
93
- wd , err := os .Getwd ()
94
- if err != nil {
95
- t .Fatal (err )
96
- }
97
- defer func () {
98
- if err = os .Chdir (wd ); err != nil {
99
- t .Fatal (err )
100
- }
101
- }()
102
- if err = os .Chdir (tfDir ); err != nil {
103
- t .Fatal (err )
94
+ cfg := gen.Config {
95
+ Inputs : map [string ]string {
96
+ APIsDirKey : filepath .Join (testGoDataDir , scaffold .ApisDir ),
97
+ },
104
98
}
105
-
106
- cfg := gen.Config {}
107
99
g := NewCRDGo (cfg )
108
100
fileMap , err := g .(crdGenerator ).generateGo ()
109
101
if err != nil {
@@ -121,9 +113,6 @@ func TestCRDGo(t *testing.T) {
121
113
}
122
114
123
115
func TestCRDNonGo (t * testing.T ) {
124
- tfDir := getTestFrameworkPath (t )
125
- tfCRDsDir := filepath .Join (tfDir , "deploy" , "crds" )
126
-
127
116
cases := []struct {
128
117
description string
129
118
apiVersion , kind string
@@ -133,11 +122,11 @@ func TestCRDNonGo(t *testing.T) {
133
122
}{
134
123
{
135
124
"non-existent CRD with default structural schema" ,
136
- "cache.example.com/v1alpha1" , "Memcached" , filepath .Join ("not" , "exist" ), crdNonGoDefaultExp , false ,
125
+ testAPIVersion , testKind , filepath .Join ("not" , "exist" ), crdNonGoDefaultExp , false ,
137
126
},
138
127
{
139
128
"existing CRD with custom structural schema" ,
140
- "cache.example.com/v1alpha1" , "Memcached" , tfCRDsDir , crdCustomExp , false ,
129
+ testAPIVersion , testKind , filepath . Join ( testGoDataDir , scaffold . CRDsDir ) , crdCustomExp , false ,
141
130
},
142
131
}
143
132
@@ -252,16 +241,3 @@ spec:
252
241
served: true
253
242
storage: true
254
243
`
255
-
256
- // getTestFrameworkPath constructs the path to the SDK's test-framework,
257
- // which containsa mock operator for testing, from the working directory path.
258
- func getTestFrameworkPath (t * testing.T ) string {
259
- t .Helper ()
260
- absPath , err := os .Getwd ()
261
- if err != nil {
262
- t .Fatal (err )
263
- }
264
- absPath = absPath [:strings .Index (absPath , "internal" )]
265
- tfDir := filepath .Join (absPath , "test" , "test-framework" )
266
- return tfDir
267
- }
0 commit comments