github.com/fnproject/cli@v0.0.0-20240508150455-e5d88bd86117/test/cli_docker_runtime_test.go (about)

     1  /*
     2   * Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
     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 test
    18  
    19  import (
    20  	"github.com/fnproject/cli/testharness"
    21  	"testing"
    22  )
    23  
    24  const dockerFile = `FROM golang:latest
    25  FROM fnproject/go:dev as build-stage
    26  WORKDIR /function
    27  WORKDIR /go/src/func/
    28  ENV GO111MODULE=on
    29  COPY . .
    30  RUN go build -o func -v
    31  FROM fnproject/go
    32  WORKDIR /function
    33  COPY --from=build-stage /go/src/func/func /function/
    34  ENTRYPOINT ["./func"]
    35  `
    36  
    37  const funcYaml = `name: fn_test_hello_docker_runtime
    38  version: 0.0.1
    39  runtime: docker`
    40  
    41  func withGoFunction(h *testharness.CLIHarness) {
    42  
    43  	h.CopyFiles(map[string]string{
    44  		"simplefunc/vendor":  "vendor",
    45  		"simplefunc/func.go": "func.go",
    46  		"simplefunc/go.sum":  "go.sum",
    47  		"simplefunc/go.mod":  "go.mod",
    48  	})
    49  }
    50  
    51  func TestDockerRuntimeInit(t *testing.T) {
    52  	t.Parallel()
    53  	tctx := testharness.Create(t)
    54  	defer tctx.Cleanup()
    55  
    56  	appName := tctx.NewAppName()
    57  	tctx.Fn("create", "app", appName).AssertSuccess()
    58  	fnName := tctx.NewFuncName(appName)
    59  	tctx.MkDir(fnName)
    60  	tctx.Cd(fnName)
    61  	withGoFunction(tctx)
    62  	tctx.WithFile("Dockerfile", dockerFile, 0644)
    63  
    64  	tctx.Fn("init").AssertSuccess()
    65  	tctx.Fn("--verbose", "build").AssertSuccess()
    66  	tctx.Fn("--registry", "test", "deploy", "--local", "--app", appName).AssertSuccess()
    67  	tctx.Fn("invoke", appName, fnName).AssertSuccess()
    68  }
    69  
    70  func TestDockerRuntimeBuildFailsWithNoDockerfile(t *testing.T) {
    71  	t.Parallel()
    72  	tctx := testharness.Create(t)
    73  	defer tctx.Cleanup()
    74  
    75  	appName := tctx.NewAppName()
    76  	fnName := tctx.NewFuncName(appName)
    77  	tctx.MkDir(fnName)
    78  	tctx.Cd(fnName)
    79  	withGoFunction(tctx)
    80  	tctx.WithFile("func.yaml", funcYaml, 0644)
    81  
    82  	tctx.Fn("--verbose", "build").AssertFailed().AssertStderrContains("Dockerfile does not exist")
    83  
    84  }