From 0696bf3885c0cf37f1ab5bd5d0b83433a9553443 Mon Sep 17 00:00:00 2001 From: Tibor Vass Date: Thu, 10 Oct 2019 23:15:25 +0000 Subject: [PATCH] executor/oci: add TestResolvConfNotExist Signed-off-by: Tibor Vass --- executor/oci/resolvconf.go | 5 ++++- executor/oci/resolvconf_test.go | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 executor/oci/resolvconf_test.go diff --git a/executor/oci/resolvconf.go b/executor/oci/resolvconf.go index 3d568f7b..ba96312a 100644 --- a/executor/oci/resolvconf.go +++ b/executor/oci/resolvconf.go @@ -16,6 +16,9 @@ var g flightcontrol.Group var notFirstRun bool var lastNotEmpty bool +// overridden by tests +var resolvconfGet = resolvconf.Get + type DNSConfig struct { Nameservers []string Options []string @@ -59,7 +62,7 @@ func GetResolvConf(ctx context.Context, stateDir string, idmap *idtools.Identity } var dt []byte - f, err := resolvconf.Get() + f, err := resolvconfGet() if err != nil { if !os.IsNotExist(err) { return "", err diff --git a/executor/oci/resolvconf_test.go b/executor/oci/resolvconf_test.go new file mode 100644 index 00000000..3291be49 --- /dev/null +++ b/executor/oci/resolvconf_test.go @@ -0,0 +1,39 @@ +package oci + +import ( + "context" + "io/ioutil" + "os" + "testing" + + "github.com/docker/libnetwork/resolvconf" + "github.com/stretchr/testify/require" +) + +// TestResolvConfNotExist modifies a global variable +// It must not run in parallel. +func TestResolvConfNotExist(t *testing.T) { + oldResolvconfGet := resolvconfGet + defer func() { + resolvconfGet = oldResolvconfGet + }() + resolvconfGet = func() (*resolvconf.File, error) { + return nil, os.ErrNotExist + } + + defaultResolvConf := ` +nameserver 8.8.8.8 +nameserver 8.8.4.4 +nameserver 2001:4860:4860::8888 +nameserver 2001:4860:4860::8844` + + dir, err := ioutil.TempDir("", "buildkit-test") + require.NoError(t, err) + defer os.RemoveAll(dir) + ctx := context.Background() + p, err := GetResolvConf(ctx, dir, nil, nil) + require.NoError(t, err) + b, err := ioutil.ReadFile(p) + require.NoError(t, err) + require.Equal(t, string(b), defaultResolvConf) +}