github.com/fnproject/cli@v0.0.0-20240508150455-e5d88bd86117/test/cli_runtime_fallback_build_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  	"fmt"
    21  	"github.com/fnproject/cli/langs"
    22  	"github.com/fnproject/cli/testharness"
    23  	"strings"
    24  	"testing"
    25  )
    26  
    27  const (
    28  	rubySrcBoilerplate = `require 'fdk'
    29  def myfunction(context:, input:)
    30    "ruby#{RUBY_VERSION}"
    31  end
    32  
    33  FDK.handle(target: :myfunction)
    34  `
    35  
    36  	rubyGemfileBoilerplate = `source 'https://rubygems.org' do
    37    gem 'fdk', '>= %s'
    38  end
    39  `
    40  	funcYamlContent = `schema_version: 20180708
    41  name: %s 
    42  version: 0.0.1
    43  runtime: ruby 
    44  entrypoint: ruby func.rb`
    45  )
    46  
    47  /*
    48  	This test case check for backwards compatibility with older cli func.yaml file
    49  	Cases Tested:
    50  	1. During `fn build` make sure Build_image and Run_image are stamped in func.yaml file
    51  	2. Function container is build using proper fallback runtime and dev image, check
    52  		by invoking container and fetching runtime version, should match with fallback version.
    53  */
    54  func TestFnBuildWithOlderRuntimeWithoutVersion(t *testing.T) {
    55  	t.Run("`fn invoke` should return the fallback ruby version", func(t *testing.T) {
    56  		t.Parallel()
    57  		h := testharness.Create(t)
    58  		defer h.Cleanup()
    59  
    60  		appName := h.NewAppName()
    61  		funcName := h.NewFuncName(appName)
    62  		dirName := funcName + "_dir"
    63  		fmt.Println(appName + " " + funcName)
    64  		h.Fn("create", "app", appName).AssertSuccess()
    65  		h.Fn("init", "--runtime", "ruby", "--name", funcName, dirName).AssertSuccess()
    66  
    67  		// change dir to newly created function dir
    68  		h.Cd(dirName)
    69  
    70  		// write custom function file which returns runtime version
    71  		h.WithFile("func.rb", rubySrcBoilerplate, 0644)
    72  
    73  		// inject func name in yaml file placeholder
    74  		oldClientYamlFile := fmt.Sprintf(funcYamlContent, funcName)
    75  
    76  		//update back yaml file
    77  		h.WithFile("func.yaml", oldClientYamlFile, 0644)
    78  
    79  		fallBackHandler := langs.GetFallbackLangHelper("ruby")
    80  		fallBackVersion := fallBackHandler.LangStrings()[1]
    81  
    82  		h.Fn("--verbose", "build").AssertSuccess()
    83  
    84  		bi, err := fallBackHandler.BuildFromImage()
    85  		if err != nil {
    86  			panic(err)
    87  		}
    88  		ri, err := fallBackHandler.RunFromImage()
    89  		if err != nil {
    90  			panic(err)
    91  		}
    92  
    93  		updatedFuncFile := h.GetYamlFile("func.yaml")
    94  		if bi == "" || ri == "" {
    95  			err := "Build_image or Run_image property is not set in func.yaml file"
    96  			panic(err)
    97  		}
    98  
    99  		//Test whether build_image set in func.yaml is correct or not
   100  		if bi != updatedFuncFile.Build_image {
   101  			err := fmt.Sprintf("Expected Build image %s and Build image in func.yaml do not match%s", bi, updatedFuncFile.Build_image)
   102  			panic(err)
   103  		}
   104  
   105  		//Test whether run_image set in func.yaml is correct or not
   106  		if ri != updatedFuncFile.Run_image {
   107  			err := fmt.Sprintf("Expected Run image %s and Run image in func.yaml do not match%s", ri, updatedFuncFile.Run_image)
   108  			panic(err)
   109  		}
   110  
   111  		h.Fn("--registry", "test", "deploy", "--local", "--app", appName).AssertSuccess()
   112  		result := h.Fn("invoke", appName, funcName).AssertSuccess()
   113  
   114  		// get the returned version from ruby image
   115  		imageVersion := result.Stdout
   116  
   117  		fmt.Println("Ruby version returned by image :" + imageVersion)
   118  		fmt.Println("Fallback ruby version :" + fallBackVersion)
   119  		match := strings.Contains(imageVersion, fallBackVersion)
   120  		if !match {
   121  			err := fmt.Sprintf("Versions do not match, `ruby` image version %s does not match with fallback version %s", imageVersion, fallBackVersion)
   122  			panic(err)
   123  		}
   124  	})
   125  }