github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/buildpacks/lifecycle_test.go (about)

     1  /*
     2  Copyright 2019 The Skaffold 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 buildpacks
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"testing"
    23  
    24  	lifecycle "github.com/buildpacks/lifecycle/cmd"
    25  	pack "github.com/buildpacks/pack/pkg/client"
    26  
    27  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    28  	"github.com/GoogleContainerTools/skaffold/testutil"
    29  )
    30  
    31  func TestLifecycleStatusCode(t *testing.T) {
    32  	tests := []struct {
    33  		code     int
    34  		expected string
    35  	}{
    36  		{lifecycle.CodeFailed, "buildpacks lifecycle failed"},
    37  		{lifecycle.CodeInvalidArgs, "lifecycle reported invalid arguments"},
    38  		{lifecycle.CodeIncompatiblePlatformAPI, "incompatible version of Platform API"},
    39  		{lifecycle.CodeIncompatibleBuildpackAPI, "incompatible version of Buildpacks API"},
    40  
    41  		{0, "lifecycle failed with status code 0"},
    42  	}
    43  	for _, test := range tests {
    44  		result := mapLifecycleStatusCode(test.code)
    45  		if result != test.expected {
    46  			t.Errorf("code %d: got %q, wanted %q", test.code, result, test.expected)
    47  		}
    48  	}
    49  	for _, test := range tests {
    50  		errText := fmt.Sprintf("failed with status code: %d", test.code)
    51  		result := rewriteLifecycleStatusCode(errors.New(errText))
    52  		if result.Error() != test.expected {
    53  			t.Errorf("got %q, wanted %q", result.Error(), test.expected)
    54  		}
    55  	}
    56  }
    57  
    58  func TestContainerConfig(t *testing.T) {
    59  	tests := []struct {
    60  		description string
    61  		volumes     []latest.BuildpackVolume
    62  		shouldErr   bool
    63  		expected    pack.ContainerConfig
    64  	}{
    65  		{
    66  			description: "single volume with no options",
    67  			volumes:     []latest.BuildpackVolume{{Host: "/foo", Target: "/bar"}},
    68  			expected:    pack.ContainerConfig{Volumes: []string{"/foo:/bar"}},
    69  		},
    70  		{
    71  			description: "single volume with  options",
    72  			volumes:     []latest.BuildpackVolume{{Host: "/foo", Target: "/bar", Options: "rw"}},
    73  			expected:    pack.ContainerConfig{Volumes: []string{"/foo:/bar:rw"}},
    74  		},
    75  		{
    76  			description: "multiple volumes",
    77  			volumes: []latest.BuildpackVolume{
    78  				{Host: "/foo", Target: "/bar", Options: "rw"},
    79  				{Host: "/bat", Target: "/baz", Options: "ro"},
    80  			},
    81  			expected: pack.ContainerConfig{Volumes: []string{"/foo:/bar:rw", "/bat:/baz:ro"}},
    82  		},
    83  		{
    84  			description: "missing host is skipped",
    85  			volumes:     []latest.BuildpackVolume{{Host: "", Target: "/bar"}},
    86  			shouldErr:   true,
    87  		},
    88  		{
    89  			description: "missing target is skipped",
    90  			volumes:     []latest.BuildpackVolume{{Host: "/foo", Target: ""}},
    91  			shouldErr:   true,
    92  		},
    93  	}
    94  
    95  	for _, test := range tests {
    96  		testutil.Run(t, test.description, func(t *testutil.T) {
    97  			artifact := latest.BuildpackArtifact{
    98  				Volumes: &test.volumes,
    99  			}
   100  			result, err := containerConfig(&artifact)
   101  			t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expected, result)
   102  		})
   103  	}
   104  }