|
| 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 | +} |
0 commit comments