Skip to content

Commit

Permalink
Merge pull request #260 from etiennecoutaud/refactor-gen-test
Browse files Browse the repository at this point in the history
pkg/generator: Refactor generator_test.go
  • Loading branch information
fanminshi authored May 17, 2018
2 parents 67e438f + 00b11c4 commit 6623f0b
Show file tree
Hide file tree
Showing 9 changed files with 518 additions and 379 deletions.
68 changes: 68 additions & 0 deletions pkg/generator/gen_api_register_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2018 The Operator-SDK 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 generator

import (
"bytes"
"testing"
)

const registerExp = `package v1alpha1
import (
sdkK8sutil "github.com/operator-framework/operator-sdk/pkg/util/k8sutil"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
const (
version = "v1alpha1"
groupName = "app.example.com"
)
var (
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
AddToScheme = SchemeBuilder.AddToScheme
// SchemeGroupVersion is the group version used to register these objects.
SchemeGroupVersion = schema.GroupVersion{Group: groupName, Version: version}
)
func init() {
sdkK8sutil.AddToSDKScheme(AddToScheme)
}
// addKnownTypes adds the set of types defined in this package to the supplied scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
&AppService{},
&AppServiceList{},
)
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
return nil
}
`

func TestGenRegister(t *testing.T) {
buf := &bytes.Buffer{}
if err := renderAPIRegisterFile(buf, appKind, appGroupName, appVersion); err != nil {
t.Error(err)
return
}
if registerExp != buf.String() {
t.Errorf("want %v, got %v", registerExp, buf.String())
}
}
62 changes: 62 additions & 0 deletions pkg/generator/gen_api_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2018 The Operator-SDK 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 generator

import (
"bytes"
"testing"
)

const typesExp = `package v1alpha1
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type PlayServiceList struct {
metav1.TypeMeta ` + "`" + `json:",inline"` + "`\n" +
` metav1.ListMeta ` + "`" + `json:"metadata"` + "`\n" +
` Items []PlayService ` + "`" + `json:"items"` + "`" + `
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type PlayService struct {
metav1.TypeMeta ` + "`" + `json:",inline"` + "`\n" +
` metav1.ObjectMeta ` + "`" + `json:"metadata"` + "`\n" +
` Spec PlayServiceSpec ` + "`" + `json:"spec"` + "`\n" +
` Status PlayServiceStatus ` + "`" + `json:"status,omitempty"` + "`" + `
}
type PlayServiceSpec struct {
// Fill me
}
type PlayServiceStatus struct {
// Fill me
}
`

func TestGenTypes(t *testing.T) {
buf := &bytes.Buffer{}
if err := renderAPITypesFile(buf, "PlayService", "v1alpha1"); err != nil {
t.Error(err)
return
}
if typesExp != buf.String() {
t.Errorf("want %v, got %v", typesExp, buf.String())
}
}
77 changes: 77 additions & 0 deletions pkg/generator/gen_build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2018 The Operator-SDK 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 generator

import (
"bytes"
"testing"
)

const buildExp = `#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
if ! which go > /dev/null; then
echo "golang needs to be installed"
exit 1
fi
BIN_DIR="$(pwd)/tmp/_output/bin"
mkdir -p ${BIN_DIR}
PROJECT_NAME="app-operator"
REPO_PATH="github.com/example-inc/app-operator"
BUILD_PATH="${REPO_PATH}/cmd/${PROJECT_NAME}"
echo "building "${PROJECT_NAME}"..."
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o ${BIN_DIR}/${PROJECT_NAME} $BUILD_PATH
`

const dockerFileExp = `FROM alpine:3.6
RUN adduser -D app-operator
USER app-operator
ADD tmp/_output/bin/app-operator /usr/local/bin/app-operator
`

func TestGenBuild(t *testing.T) {
buf := &bytes.Buffer{}
if err := renderBuildFile(buf, appRepoPath, appProjectName); err != nil {
t.Error(err)
return
}
if buildExp != buf.String() {
t.Errorf("want %v, got %v", buildExp, buf.String())
}

buf = &bytes.Buffer{}
if err := renderDockerBuildFile(buf); err != nil {
t.Error(err)
return
}
if dockerBuildTmpl != buf.String() {
t.Errorf("want %v, got %v", dockerBuildTmpl, buf.String())
}

buf = &bytes.Buffer{}
if err := renderDockerFile(buf, appProjectName); err != nil {
t.Error(err)
return
}
if dockerFileExp != buf.String() {
t.Errorf("want %v, got %v", dockerFileExp, buf.String())
}
}
65 changes: 65 additions & 0 deletions pkg/generator/gen_codegen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2018 The Operator-SDK 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 generator

import (
"bytes"
"testing"
)

const boilerplateExp = `
`

const updateGeneratedExp = `#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
DOCKER_REPO_ROOT="/go/src/github.com/example-inc/app-operator"
IMAGE=${IMAGE:-"gcr.io/coreos-k8s-scale-testing/codegen:1.9.3"}
docker run --rm \
-v "$PWD":"$DOCKER_REPO_ROOT":Z \
-w "$DOCKER_REPO_ROOT" \
"$IMAGE" \
"/go/src/k8s.io/code-generator/generate-groups.sh" \
"deepcopy" \
"github.com/example-inc/app-operator/pkg/generated" \
"github.com/example-inc/app-operator/pkg/apis" \
"app:v1alpha1" \
--go-header-file "./tmp/codegen/boilerplate.go.txt" \
$@
`

func TestCodeGen(t *testing.T) {
buf := &bytes.Buffer{}
if err := renderBoilerplateFile(buf, appProjectName); err != nil {
t.Error(err)
return
}
if boilerplateExp != buf.String() {
t.Errorf("want %v, got %v", boilerplateExp, buf.String())
}

buf = &bytes.Buffer{}
if err := renderUpdateGeneratedFile(buf, appRepoPath, appApiDirName, appVersion); err != nil {
t.Error(err)
return
}
if updateGeneratedExp != buf.String() {
t.Errorf("want %v, got %v", updateGeneratedExp, buf.String())
}
}
35 changes: 35 additions & 0 deletions pkg/generator/gen_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2018 The Operator-SDK 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 generator

import (
"bytes"
"testing"
)

const configExp = `apiVersion: app.example.com/v1alpha1
kind: AppService
projectName: app-operator
`

func TestGenConfig(t *testing.T) {
buf := &bytes.Buffer{}
if err := renderConfigFile(buf, appAPIVersion, appKind, appProjectName); err != nil {
t.Error(err)
}
if configExp != buf.String() {
t.Errorf("want %v, got %v", configExp, buf.String())
}
}
41 changes: 41 additions & 0 deletions pkg/generator/gen_deps_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2018 The Operator-SDK 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 generator

import (
"bytes"
"testing"
)

func TestGenGopkg(t *testing.T) {
buf := &bytes.Buffer{}
if err := renderGopkgTomlFile(buf); err != nil {
t.Error(err)
return
}

if gopkgTomlTmpl != buf.String() {
t.Errorf("want %v, got %v", gopkgTomlTmpl, buf.String())
}

buf = &bytes.Buffer{}
if err := renderGopkgLockFile(buf); err != nil {
t.Error(err)
return
}
if gopkgLockTmpl != buf.String() {
t.Errorf("want %v, got %v", gopkgLockTmpl, buf.String())
}
}
Loading

0 comments on commit 6623f0b

Please sign in to comment.