github.com/hairyhenderson/gomplate/v3@v3.11.7/internal/tests/integration/datasources_git_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"path/filepath"
     7  	"strconv"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/require"
    12  	"gotest.tools/v3/fs"
    13  	"gotest.tools/v3/icmd"
    14  )
    15  
    16  func setupDatasourcesGitTest(t *testing.T) *fs.Dir {
    17  	tmpDir := fs.NewDir(t, "gomplate-inttests",
    18  		fs.WithFiles(map[string]string{
    19  			"config.json": `{"foo": {"bar": "baz"}}`,
    20  		}),
    21  	)
    22  	t.Cleanup(tmpDir.Remove)
    23  
    24  	repoPath := tmpDir.Join("repo")
    25  
    26  	result := icmd.RunCommand("git", "init", repoPath)
    27  	result.Assert(t, icmd.Expected{ExitCode: 0, Out: "Initialized empty Git repository"})
    28  
    29  	result = icmd.RunCommand("mv", tmpDir.Join("config.json"), repoPath)
    30  	result.Assert(t, icmd.Expected{ExitCode: 0})
    31  
    32  	result = icmd.RunCmd(icmd.Command("git", "add", "config.json"), icmd.Dir(repoPath))
    33  	result.Assert(t, icmd.Expected{ExitCode: 0})
    34  
    35  	result = icmd.RunCmd(icmd.Command("git", "commit", "-m", "Initial commit"), icmd.Dir(repoPath))
    36  	result.Assert(t, icmd.Expected{ExitCode: 0})
    37  
    38  	return tmpDir
    39  }
    40  
    41  func startGitDaemon(t *testing.T) string {
    42  	tmpDir := setupDatasourcesGitTest(t)
    43  
    44  	pidDir := fs.NewDir(t, "gomplate-inttests-pid")
    45  	t.Cleanup(pidDir.Remove)
    46  
    47  	port, addr := freeport(t)
    48  	gitDaemon := icmd.Command("git", "daemon",
    49  		"--verbose",
    50  		"--port="+strconv.Itoa(port),
    51  		"--base-path="+tmpDir.Path(),
    52  		"--pid-file="+pidDir.Join("git.pid"),
    53  		"--export-all",
    54  		tmpDir.Join("repo", ".git"),
    55  	)
    56  	gitDaemon.Stdin = nil
    57  	gitDaemon.Stdout = &bytes.Buffer{}
    58  	gitDaemon.Dir = tmpDir.Path()
    59  	result := icmd.StartCmd(gitDaemon)
    60  
    61  	t.Cleanup(func() {
    62  		err := result.Cmd.Process.Kill()
    63  		require.NoError(t, err)
    64  
    65  		_, err = result.Cmd.Process.Wait()
    66  		require.NoError(t, err)
    67  
    68  		result.Assert(t, icmd.Expected{ExitCode: 0})
    69  	})
    70  
    71  	// give git time to start
    72  	time.Sleep(500 * time.Millisecond)
    73  
    74  	return addr
    75  }
    76  
    77  func TestDatasources_GitFileDatasource(t *testing.T) {
    78  	tmpDir := setupDatasourcesGitTest(t)
    79  
    80  	u := filepath.ToSlash(tmpDir.Join("repo"))
    81  	o, e, err := cmd(t,
    82  		"-d", "config=git+file://"+u+"//config.json",
    83  		"-i", `{{ (datasource "config").foo.bar }}`,
    84  	).run()
    85  	assertSuccess(t, o, e, err, "baz")
    86  
    87  	o, e, err = cmd(t,
    88  		"-d", "repo=git+file://"+u,
    89  		"-i", `{{ (datasource "repo" "//config.json?type=application/json" ).foo.bar }}`,
    90  	).run()
    91  	assertSuccess(t, o, e, err, "baz")
    92  
    93  	o, e, err = cmd(t,
    94  		"-d", "repo=git+file://"+u,
    95  		"-i", `{{ (datasource "repo" "//config.json" ).foo.bar }}`,
    96  	).run()
    97  	assertSuccess(t, o, e, err, "baz")
    98  }
    99  
   100  func TestDatasources_GitDatasource(t *testing.T) {
   101  	if isWindows {
   102  		t.Skip("not going to run git daemon on Windows")
   103  	}
   104  
   105  	addr := startGitDaemon(t)
   106  
   107  	o, e, err := cmd(t,
   108  		"-c", "config=git://"+addr+"/repo//config.json",
   109  		"-i", `{{ .config.foo.bar}}`,
   110  	).run()
   111  	assertSuccess(t, o, e, err, "baz")
   112  }
   113  
   114  func TestDatasources_GitHTTPDatasource(t *testing.T) {
   115  	o, e, err := cmd(t,
   116  		"-c", "short=git+https://github.com/git-fixtures/basic//json/short.json",
   117  		"-i", `{{ .short.glossary.title}}`,
   118  	).run()
   119  	assertSuccess(t, o, e, err, "example glossary")
   120  
   121  	// and one with a default branch of 'main'
   122  	o, e, err = cmd(t,
   123  		"-c", "data=git+https://github.com/hairyhenderson/git-fixtures.git//small_test.json",
   124  		"-i", `{{ .data.foo}}`,
   125  	).run()
   126  	assertSuccess(t, o, e, err, "bar")
   127  }
   128  
   129  func TestDatasources_GitSSHDatasource(t *testing.T) {
   130  	if os.Getenv("SSH_AUTH_SOCK") == "" {
   131  		t.Skip("SSH Agent not running")
   132  	}
   133  	o, e, err := cmd(t,
   134  		"-c", "short=git+ssh://git@github.com/git-fixtures/basic//json/short.json",
   135  		"-i", `{{ .short.glossary.title}}`,
   136  	).run()
   137  	assertSuccess(t, o, e, err, "example glossary")
   138  }