From 50151b347c5c9dd8a4531244c5b17615dd1ac4ed Mon Sep 17 00:00:00 2001 From: Achilleas Koutsou Date: Mon, 23 Sep 2024 16:45:06 +0200 Subject: [PATCH] cmd/otk-resolve-ostree-commit: test the mock resolver --- cmd/otk-resolve-ostree-commit/export_test.go | 13 ++++++++ cmd/otk-resolve-ostree-commit/main_test.go | 32 ++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/cmd/otk-resolve-ostree-commit/export_test.go b/cmd/otk-resolve-ostree-commit/export_test.go index 77c5f1a937..2de3e0d412 100644 --- a/cmd/otk-resolve-ostree-commit/export_test.go +++ b/cmd/otk-resolve-ostree-commit/export_test.go @@ -3,3 +3,16 @@ package main var ( Run = run ) + +func MockEnvLookup() (restore func()) { + saved := osLookupEnv + osLookupEnv = func(key string) (string, bool) { + if key == "OTK_UNDER_TEST" { + return "1", true + } + return "", false + } + return func() { + osLookupEnv = saved + } +} diff --git a/cmd/otk-resolve-ostree-commit/main_test.go b/cmd/otk-resolve-ostree-commit/main_test.go index 841ff5622f..2139dcfb43 100644 --- a/cmd/otk-resolve-ostree-commit/main_test.go +++ b/cmd/otk-resolve-ostree-commit/main_test.go @@ -191,3 +191,35 @@ func TestResolverErrors(t *testing.T) { }) } } + +func TestMockResolve(t *testing.T) { + restore := resolver.MockEnvLookup() + defer restore() + + assert := assert.New(t) + + inputReq := ` +{ + "tree": { + "ref": "otk/ostree/test", + "url": "https://ostree.example.org/repo" + } +} +` + expOutput := `{ + "tree": { + "const": { + "ref": "otk/ostree/test", + "url": "https://ostree.example.org/repo", + "checksum": "5c1c0655481926623eca85109dd4017c8dba320ea1473c42b43005db6d900a60" + } + } +} +` + input := bytes.NewBuffer([]byte(inputReq)) + output := &bytes.Buffer{} + + assert.NoError(resolver.Run(input, output)) + + assert.Equal(expOutput, output.String()) +}