github.com/projectriff/riff-cli@v0.0.5-0.20180301104501-5db7a3bd9fc1/pkg/initializers/python/dockerfile_generator_test.go (about)

     1  /*
     2   * Copyright 2018 the original author or authors.
     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 python
    18  
    19  import (
    20  	"github.com/projectriff/riff-cli/pkg/options"
    21  	"fmt"
    22  	"testing"
    23  	"github.com/stretchr/testify/assert"
    24  	"strings"
    25  )
    26  
    27  func TestPythonDockerfile(t *testing.T) {
    28  	as := assert.New(t)
    29  
    30  	opts := options.InitOptions{
    31  		Artifact:    "demo.py",
    32  		RiffVersion: "0.0.3",
    33  		FilePath:    "test_dir/python/demo",
    34  		Handler:     "process",
    35  	}
    36  
    37  	docker, err := generatePythonFunctionDockerFile(opts)
    38  	as.NoError(err)
    39  	lines := strings.Split(strings.TrimSpace(docker),"\n")
    40  	as.Equal(5, len(lines))
    41  	as.Contains(lines, fmt.Sprintf("FROM projectriff/python2-function-invoker:%s", opts.RiffVersion))
    42  	as.Contains(lines, fmt.Sprintf("ARG FUNCTION_MODULE=%s", opts.Artifact))
    43  	as.Contains(lines, fmt.Sprintf("ARG FUNCTION_HANDLER=%s", opts.Handler))
    44  	as.Contains(lines, fmt.Sprintf("ADD ./%s /", opts.Artifact))
    45  	as.NotContains(docker, "requirements.txt")
    46  	as.NotContains(docker, "pip")
    47  
    48  	as.Contains(lines,"ENV FUNCTION_URI file:///${FUNCTION_MODULE}?handler=${FUNCTION_HANDLER}")
    49  
    50  }
    51  
    52  func TestPythonWithRequirementsDockerfile(t *testing.T) {
    53  	as := assert.New(t)
    54  
    55  	opts := options.InitOptions{
    56  		Artifact:    "demo.py",
    57  		RiffVersion: "0.0.3",
    58  		FilePath:    "../../../test_data/python/demo_with_deps",
    59  		Handler:     "process",
    60  	}
    61  
    62  	docker, err := generatePythonFunctionDockerFile(opts)
    63  	as.NoError(err)
    64  	lines := strings.Split(strings.TrimSpace(docker),"\n")
    65  	as.Equal(7, len(lines))
    66  	as.Contains(lines, fmt.Sprintf("FROM projectriff/python2-function-invoker:%s", opts.RiffVersion))
    67  	as.Contains(lines, fmt.Sprintf("ARG FUNCTION_MODULE=%s", opts.Artifact))
    68  	as.Contains(lines, fmt.Sprintf("ARG FUNCTION_HANDLER=%s", opts.Handler))
    69  	as.Contains(lines, fmt.Sprintf("ADD ./%s /", opts.Artifact))
    70  	as.Contains(lines, "ADD ./requirements.txt /")
    71  	as.Contains(lines,"RUN  pip install --upgrade pip && pip install -r /requirements.txt")
    72  	as.Contains(lines,"ENV FUNCTION_URI file:///${FUNCTION_MODULE}?handler=${FUNCTION_HANDLER}")
    73  
    74  
    75  
    76  }