Skip to content

Commit 6623f0b

Browse files
authoredMay 17, 2018
Merge pull request #260 from etiennecoutaud/refactor-gen-test
pkg/generator: Refactor generator_test.go
2 parents 67e438f + 00b11c4 commit 6623f0b

9 files changed

+518
-379
lines changed
 
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2018 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package generator
16+
17+
import (
18+
"bytes"
19+
"testing"
20+
)
21+
22+
const registerExp = `package v1alpha1
23+
24+
import (
25+
sdkK8sutil "github.com/operator-framework/operator-sdk/pkg/util/k8sutil"
26+
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
"k8s.io/apimachinery/pkg/runtime"
29+
"k8s.io/apimachinery/pkg/runtime/schema"
30+
)
31+
32+
const (
33+
version = "v1alpha1"
34+
groupName = "app.example.com"
35+
)
36+
37+
var (
38+
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
39+
AddToScheme = SchemeBuilder.AddToScheme
40+
// SchemeGroupVersion is the group version used to register these objects.
41+
SchemeGroupVersion = schema.GroupVersion{Group: groupName, Version: version}
42+
)
43+
44+
func init() {
45+
sdkK8sutil.AddToSDKScheme(AddToScheme)
46+
}
47+
48+
// addKnownTypes adds the set of types defined in this package to the supplied scheme.
49+
func addKnownTypes(scheme *runtime.Scheme) error {
50+
scheme.AddKnownTypes(SchemeGroupVersion,
51+
&AppService{},
52+
&AppServiceList{},
53+
)
54+
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
55+
return nil
56+
}
57+
`
58+
59+
func TestGenRegister(t *testing.T) {
60+
buf := &bytes.Buffer{}
61+
if err := renderAPIRegisterFile(buf, appKind, appGroupName, appVersion); err != nil {
62+
t.Error(err)
63+
return
64+
}
65+
if registerExp != buf.String() {
66+
t.Errorf("want %v, got %v", registerExp, buf.String())
67+
}
68+
}

‎pkg/generator/gen_api_types_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2018 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package generator
16+
17+
import (
18+
"bytes"
19+
"testing"
20+
)
21+
22+
const typesExp = `package v1alpha1
23+
24+
import (
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
)
27+
28+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
29+
30+
type PlayServiceList struct {
31+
metav1.TypeMeta ` + "`" + `json:",inline"` + "`\n" +
32+
` metav1.ListMeta ` + "`" + `json:"metadata"` + "`\n" +
33+
` Items []PlayService ` + "`" + `json:"items"` + "`" + `
34+
}
35+
36+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
37+
38+
type PlayService struct {
39+
metav1.TypeMeta ` + "`" + `json:",inline"` + "`\n" +
40+
` metav1.ObjectMeta ` + "`" + `json:"metadata"` + "`\n" +
41+
` Spec PlayServiceSpec ` + "`" + `json:"spec"` + "`\n" +
42+
` Status PlayServiceStatus ` + "`" + `json:"status,omitempty"` + "`" + `
43+
}
44+
45+
type PlayServiceSpec struct {
46+
// Fill me
47+
}
48+
type PlayServiceStatus struct {
49+
// Fill me
50+
}
51+
`
52+
53+
func TestGenTypes(t *testing.T) {
54+
buf := &bytes.Buffer{}
55+
if err := renderAPITypesFile(buf, "PlayService", "v1alpha1"); err != nil {
56+
t.Error(err)
57+
return
58+
}
59+
if typesExp != buf.String() {
60+
t.Errorf("want %v, got %v", typesExp, buf.String())
61+
}
62+
}

‎pkg/generator/gen_build_test.go

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2018 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package generator
16+
17+
import (
18+
"bytes"
19+
"testing"
20+
)
21+
22+
const buildExp = `#!/usr/bin/env bash
23+
24+
set -o errexit
25+
set -o nounset
26+
set -o pipefail
27+
28+
if ! which go > /dev/null; then
29+
echo "golang needs to be installed"
30+
exit 1
31+
fi
32+
33+
BIN_DIR="$(pwd)/tmp/_output/bin"
34+
mkdir -p ${BIN_DIR}
35+
PROJECT_NAME="app-operator"
36+
REPO_PATH="github.com/example-inc/app-operator"
37+
BUILD_PATH="${REPO_PATH}/cmd/${PROJECT_NAME}"
38+
echo "building "${PROJECT_NAME}"..."
39+
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o ${BIN_DIR}/${PROJECT_NAME} $BUILD_PATH
40+
`
41+
42+
const dockerFileExp = `FROM alpine:3.6
43+
44+
RUN adduser -D app-operator
45+
USER app-operator
46+
47+
ADD tmp/_output/bin/app-operator /usr/local/bin/app-operator
48+
`
49+
50+
func TestGenBuild(t *testing.T) {
51+
buf := &bytes.Buffer{}
52+
if err := renderBuildFile(buf, appRepoPath, appProjectName); err != nil {
53+
t.Error(err)
54+
return
55+
}
56+
if buildExp != buf.String() {
57+
t.Errorf("want %v, got %v", buildExp, buf.String())
58+
}
59+
60+
buf = &bytes.Buffer{}
61+
if err := renderDockerBuildFile(buf); err != nil {
62+
t.Error(err)
63+
return
64+
}
65+
if dockerBuildTmpl != buf.String() {
66+
t.Errorf("want %v, got %v", dockerBuildTmpl, buf.String())
67+
}
68+
69+
buf = &bytes.Buffer{}
70+
if err := renderDockerFile(buf, appProjectName); err != nil {
71+
t.Error(err)
72+
return
73+
}
74+
if dockerFileExp != buf.String() {
75+
t.Errorf("want %v, got %v", dockerFileExp, buf.String())
76+
}
77+
}

‎pkg/generator/gen_codegen_test.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2018 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package generator
16+
17+
import (
18+
"bytes"
19+
"testing"
20+
)
21+
22+
const boilerplateExp = `
23+
`
24+
25+
const updateGeneratedExp = `#!/usr/bin/env bash
26+
27+
set -o errexit
28+
set -o nounset
29+
set -o pipefail
30+
31+
DOCKER_REPO_ROOT="/go/src/github.com/example-inc/app-operator"
32+
IMAGE=${IMAGE:-"gcr.io/coreos-k8s-scale-testing/codegen:1.9.3"}
33+
34+
docker run --rm \
35+
-v "$PWD":"$DOCKER_REPO_ROOT":Z \
36+
-w "$DOCKER_REPO_ROOT" \
37+
"$IMAGE" \
38+
"/go/src/k8s.io/code-generator/generate-groups.sh" \
39+
"deepcopy" \
40+
"github.com/example-inc/app-operator/pkg/generated" \
41+
"github.com/example-inc/app-operator/pkg/apis" \
42+
"app:v1alpha1" \
43+
--go-header-file "./tmp/codegen/boilerplate.go.txt" \
44+
$@
45+
`
46+
47+
func TestCodeGen(t *testing.T) {
48+
buf := &bytes.Buffer{}
49+
if err := renderBoilerplateFile(buf, appProjectName); err != nil {
50+
t.Error(err)
51+
return
52+
}
53+
if boilerplateExp != buf.String() {
54+
t.Errorf("want %v, got %v", boilerplateExp, buf.String())
55+
}
56+
57+
buf = &bytes.Buffer{}
58+
if err := renderUpdateGeneratedFile(buf, appRepoPath, appApiDirName, appVersion); err != nil {
59+
t.Error(err)
60+
return
61+
}
62+
if updateGeneratedExp != buf.String() {
63+
t.Errorf("want %v, got %v", updateGeneratedExp, buf.String())
64+
}
65+
}

‎pkg/generator/gen_config_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2018 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package generator
16+
17+
import (
18+
"bytes"
19+
"testing"
20+
)
21+
22+
const configExp = `apiVersion: app.example.com/v1alpha1
23+
kind: AppService
24+
projectName: app-operator
25+
`
26+
27+
func TestGenConfig(t *testing.T) {
28+
buf := &bytes.Buffer{}
29+
if err := renderConfigFile(buf, appAPIVersion, appKind, appProjectName); err != nil {
30+
t.Error(err)
31+
}
32+
if configExp != buf.String() {
33+
t.Errorf("want %v, got %v", configExp, buf.String())
34+
}
35+
}

‎pkg/generator/gen_deps_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2018 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package generator
16+
17+
import (
18+
"bytes"
19+
"testing"
20+
)
21+
22+
func TestGenGopkg(t *testing.T) {
23+
buf := &bytes.Buffer{}
24+
if err := renderGopkgTomlFile(buf); err != nil {
25+
t.Error(err)
26+
return
27+
}
28+
29+
if gopkgTomlTmpl != buf.String() {
30+
t.Errorf("want %v, got %v", gopkgTomlTmpl, buf.String())
31+
}
32+
33+
buf = &bytes.Buffer{}
34+
if err := renderGopkgLockFile(buf); err != nil {
35+
t.Error(err)
36+
return
37+
}
38+
if gopkgLockTmpl != buf.String() {
39+
t.Errorf("want %v, got %v", gopkgLockTmpl, buf.String())
40+
}
41+
}

0 commit comments

Comments
 (0)
Please sign in to comment.