github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/builder/dockerfile/buildargs_test.go (about)

     1  package dockerfile // import "github.com/demonoid81/moby/builder/dockerfile"
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  	"testing"
     7  
     8  	"gotest.tools/v3/assert"
     9  	is "gotest.tools/v3/assert/cmp"
    10  )
    11  
    12  func strPtr(source string) *string {
    13  	return &source
    14  }
    15  
    16  func TestGetAllAllowed(t *testing.T) {
    17  	buildArgs := NewBuildArgs(map[string]*string{
    18  		"ArgNotUsedInDockerfile":              strPtr("fromopt1"),
    19  		"ArgOverriddenByOptions":              strPtr("fromopt2"),
    20  		"ArgNoDefaultInDockerfileFromOptions": strPtr("fromopt3"),
    21  		"HTTP_PROXY":                          strPtr("theproxy"),
    22  	})
    23  
    24  	buildArgs.AddMetaArg("ArgFromMeta", strPtr("frommeta1"))
    25  	buildArgs.AddMetaArg("ArgFromMetaOverridden", strPtr("frommeta2"))
    26  	buildArgs.AddMetaArg("ArgFromMetaNotUsed", strPtr("frommeta3"))
    27  
    28  	buildArgs.AddArg("ArgOverriddenByOptions", strPtr("fromdockerfile2"))
    29  	buildArgs.AddArg("ArgWithDefaultInDockerfile", strPtr("fromdockerfile1"))
    30  	buildArgs.AddArg("ArgNoDefaultInDockerfile", nil)
    31  	buildArgs.AddArg("ArgNoDefaultInDockerfileFromOptions", nil)
    32  	buildArgs.AddArg("ArgFromMeta", nil)
    33  	buildArgs.AddArg("ArgFromMetaOverridden", strPtr("fromdockerfile3"))
    34  
    35  	all := buildArgs.GetAllAllowed()
    36  	expected := map[string]string{
    37  		"HTTP_PROXY":                          "theproxy",
    38  		"ArgOverriddenByOptions":              "fromopt2",
    39  		"ArgWithDefaultInDockerfile":          "fromdockerfile1",
    40  		"ArgNoDefaultInDockerfileFromOptions": "fromopt3",
    41  		"ArgFromMeta":                         "frommeta1",
    42  		"ArgFromMetaOverridden":               "fromdockerfile3",
    43  	}
    44  	assert.Check(t, is.DeepEqual(expected, all))
    45  }
    46  
    47  func TestGetAllMeta(t *testing.T) {
    48  	buildArgs := NewBuildArgs(map[string]*string{
    49  		"ArgNotUsedInDockerfile":        strPtr("fromopt1"),
    50  		"ArgOverriddenByOptions":        strPtr("fromopt2"),
    51  		"ArgNoDefaultInMetaFromOptions": strPtr("fromopt3"),
    52  		"HTTP_PROXY":                    strPtr("theproxy"),
    53  	})
    54  
    55  	buildArgs.AddMetaArg("ArgFromMeta", strPtr("frommeta1"))
    56  	buildArgs.AddMetaArg("ArgOverriddenByOptions", strPtr("frommeta2"))
    57  	buildArgs.AddMetaArg("ArgNoDefaultInMetaFromOptions", nil)
    58  
    59  	all := buildArgs.GetAllMeta()
    60  	expected := map[string]string{
    61  		"HTTP_PROXY":                    "theproxy",
    62  		"ArgFromMeta":                   "frommeta1",
    63  		"ArgOverriddenByOptions":        "fromopt2",
    64  		"ArgNoDefaultInMetaFromOptions": "fromopt3",
    65  	}
    66  	assert.Check(t, is.DeepEqual(expected, all))
    67  }
    68  
    69  func TestWarnOnUnusedBuildArgs(t *testing.T) {
    70  	buildArgs := NewBuildArgs(map[string]*string{
    71  		"ThisArgIsUsed":    strPtr("fromopt1"),
    72  		"ThisArgIsNotUsed": strPtr("fromopt2"),
    73  		"HTTPS_PROXY":      strPtr("referenced builtin"),
    74  		"HTTP_PROXY":       strPtr("unreferenced builtin"),
    75  	})
    76  	buildArgs.AddArg("ThisArgIsUsed", nil)
    77  	buildArgs.AddArg("HTTPS_PROXY", nil)
    78  
    79  	buffer := new(bytes.Buffer)
    80  	buildArgs.WarnOnUnusedBuildArgs(buffer)
    81  	out := buffer.String()
    82  	assert.Assert(t, !strings.Contains(out, "ThisArgIsUsed"), out)
    83  	assert.Assert(t, !strings.Contains(out, "HTTPS_PROXY"), out)
    84  	assert.Assert(t, !strings.Contains(out, "HTTP_PROXY"), out)
    85  	assert.Check(t, is.Contains(out, "ThisArgIsNotUsed"))
    86  }
    87  
    88  func TestIsUnreferencedBuiltin(t *testing.T) {
    89  	buildArgs := NewBuildArgs(map[string]*string{
    90  		"ThisArgIsUsed":    strPtr("fromopt1"),
    91  		"ThisArgIsNotUsed": strPtr("fromopt2"),
    92  		"HTTPS_PROXY":      strPtr("referenced builtin"),
    93  		"HTTP_PROXY":       strPtr("unreferenced builtin"),
    94  	})
    95  	buildArgs.AddArg("ThisArgIsUsed", nil)
    96  	buildArgs.AddArg("HTTPS_PROXY", nil)
    97  
    98  	assert.Check(t, buildArgs.IsReferencedOrNotBuiltin("ThisArgIsUsed"))
    99  	assert.Check(t, buildArgs.IsReferencedOrNotBuiltin("ThisArgIsNotUsed"))
   100  	assert.Check(t, buildArgs.IsReferencedOrNotBuiltin("HTTPS_PROXY"))
   101  	assert.Check(t, !buildArgs.IsReferencedOrNotBuiltin("HTTP_PROXY"))
   102  }