From 3ca3a34fbf59fc9fb7e41cae85a75c24f5cebb7f Mon Sep 17 00:00:00 2001
From: Kostiantyn Kalynovskyi <kkalynovskyi@mirantis.com>
Date: Wed, 30 Oct 2019 12:35:40 -0500
Subject: [PATCH] Add NewTestBundle function to testutils package

This commit adds small helper function in testutils module, that would
help with testing modules that relay on bundle interface. The function
will take directory with testdata as a parameter, and load all files in
it into its fakefile system, allowing bundle to get those files and
render required yaml/kustomize files storing them as airship documents
in the returned bundle.

Change-Id: Ib55180cc720c42cab77626601d9ec0d6151b8454
---
 pkg/document/bundle_test.go | 15 ++-------------
 testutil/testdatafs.go      | 25 +++++++++++++++----------
 2 files changed, 17 insertions(+), 23 deletions(-)

diff --git a/pkg/document/bundle_test.go b/pkg/document/bundle_test.go
index 98854f04f..a1da66f78 100644
--- a/pkg/document/bundle_test.go
+++ b/pkg/document/bundle_test.go
@@ -9,18 +9,12 @@ import (
 	"sigs.k8s.io/kustomize/v3/pkg/gvk"
 	"sigs.k8s.io/kustomize/v3/pkg/types"
 
-	"opendev.org/airship/airshipctl/pkg/document"
 	"opendev.org/airship/airshipctl/testutil"
 )
 
 func TestNewBundle(t *testing.T) {
 
-	fSys := testutil.SetupTestFs(t, "testdata")
-	bundle, err := document.NewBundle(fSys, "/", "/")
-	if err != nil {
-		t.Fatalf("Building Bundle Failed: %v", err)
-	}
-
+	bundle := testutil.NewTestBundle(t, "testdata")
 	require := require.New(t)
 	require.NotNil(bundle)
 
@@ -28,12 +22,7 @@ func TestNewBundle(t *testing.T) {
 
 func TestBundleDocumentFiltering(t *testing.T) {
 
-	fSys := testutil.SetupTestFs(t, "testdata")
-	bundle, err := document.NewBundle(fSys, "/", "/")
-	if err != nil {
-		t.Fatalf("Building Bundle Failed: %v", err)
-	}
-
+	bundle := testutil.NewTestBundle(t, "testdata")
 	assert := assert.New(t)
 
 	t.Run("GetKustomizeResourceMap", func(t *testing.T) {
diff --git a/testutil/testdatafs.go b/testutil/testdatafs.go
index 571cf3678..7a93121f4 100644
--- a/testutil/testdatafs.go
+++ b/testutil/testdatafs.go
@@ -5,7 +5,10 @@ import (
 	"path/filepath"
 	"testing"
 
+	"github.com/stretchr/testify/require"
 	"sigs.k8s.io/kustomize/v3/pkg/fs"
+
+	"opendev.org/airship/airshipctl/pkg/document"
 )
 
 // SetupTestFs help manufacture a fake file system for testing purposes. It
@@ -13,26 +16,28 @@ import (
 // to the tests themselves, and will write each of those files (preserving
 // names) to an in-memory file system and return that fs
 func SetupTestFs(t *testing.T, fixtureDir string) fs.FileSystem {
+	t.Helper()
 
 	x := fs.MakeFakeFS()
 
 	files, err := ioutil.ReadDir(fixtureDir)
-	if err != nil {
-		t.Fatalf("Unable to read fixture directory %s: %v", fixtureDir, err)
-	}
+	require.NoError(t, err, "Failed to read fixture directory, setting up testfs failed")
 	for _, file := range files {
 		fileName := file.Name()
 		filePath := filepath.Join(fixtureDir, fileName)
 		fileBytes, err := ioutil.ReadFile(filePath)
-		if err != nil {
-			t.Fatalf("Error reading fixture %s: %v", filePath, err)
-		}
-		// nolint: errcheck
+		require.NoError(t, err, "Failed to read file, setting up testfs failed")
 		err = x.WriteFile(filepath.Join("/", file.Name()), fileBytes)
-		if err != nil {
-			t.Fatalf("Error writing fixture %s: %v", filePath, err)
-		}
+		require.NoError(t, err, "Failed to write file, setting up testfs failed")
 	}
 	return x
 
 }
+
+// NewTestBundle helps to create a new bundle with FakeFs containing documents from fixtureDir
+func NewTestBundle(t *testing.T, fixtureDir string) document.Bundle {
+	t.Helper()
+	b, err := document.NewBundle(SetupTestFs(t, fixtureDir), "/", "/")
+	require.NoError(t, err, "Failed to build a bundle, setting up TestBundle failed")
+	return b
+}