github.com/ystia/yorc/v4@v4.3.0/storage/internal/file/store_test.go (about)

     1  // Copyright 2019 Bull S.A.S. Atos Technologies - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package file
    16  
    17  import (
    18  	"os"
    19  	"path"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"github.com/ystia/yorc/v4/config"
    25  	"github.com/ystia/yorc/v4/storage/store"
    26  )
    27  
    28  // The aim of this function is to run all package tests with consul server dependency with only one consul server start
    29  func TestRunFileStoragePackageTests(t *testing.T) {
    30  	cfg := store.SetupTestConfig(t)
    31  	defer func() {
    32  		os.RemoveAll(cfg.WorkingDirectory)
    33  	}()
    34  
    35  	t.Run("groupFileStore", func(t *testing.T) {
    36  		t.Run("testFileStoreWithEncryption", func(t *testing.T) {
    37  			testFileStoreWithEncryption(t, cfg)
    38  		})
    39  		t.Run("testFileStoreTypesWithEncryption", func(t *testing.T) {
    40  			testFileStoreTypesWithEncryption(t, cfg)
    41  		})
    42  		t.Run("testFileStoreWithEncryptionWithoutSecretKeyProvided", func(t *testing.T) {
    43  			testFileStoreWithEncryptionWithoutSecretKeyProvided(t, cfg)
    44  		})
    45  		t.Run("testFileStoreWithCache", func(t *testing.T) {
    46  			testFileStoreWithCache(t, cfg)
    47  		})
    48  		t.Run("testFileStoreTypesWithCache", func(t *testing.T) {
    49  			testFileStoreTypesWithCache(t, cfg)
    50  		})
    51  	})
    52  }
    53  
    54  func testFileStoreWithEncryptionWithoutSecretKeyProvided(t *testing.T, cfg config.Configuration) {
    55  	_, err := NewStore(cfg, "testStoreID", nil, false, true)
    56  	require.Error(t, err, "expected error as secrek key is missing")
    57  }
    58  
    59  func testFileStoreWithEncryption(t *testing.T, cfg config.Configuration) {
    60  	props := config.DynamicMap{
    61  		"passphrase": "myverystrongpasswordo32bitlength",
    62  		"root_dir":   path.Join(cfg.WorkingDirectory, t.Name()),
    63  	}
    64  	fileStore, err := NewStore(cfg, "testStoreID", props, false, true)
    65  	require.NoError(t, err, "failed to instantiate new store")
    66  	store.CommonStoreTest(t, fileStore)
    67  }
    68  
    69  func testFileStoreTypesWithEncryption(t *testing.T, cfg config.Configuration) {
    70  	props := config.DynamicMap{
    71  		"passphrase": "myverystrongpasswordo32bitlength",
    72  		"root_dir":   path.Join(cfg.WorkingDirectory, t.Name()),
    73  	}
    74  	fileStore, err := NewStore(cfg, "testStoreID", props, false, true)
    75  	require.NoError(t, err, "failed to instantiate new store")
    76  	store.CommonStoreTestAllTypes(t, fileStore)
    77  }
    78  
    79  func testFileStoreWithCache(t *testing.T, cfg config.Configuration) {
    80  	props := config.DynamicMap{
    81  		"root_dir": path.Join(cfg.WorkingDirectory, t.Name()),
    82  	}
    83  	fileStore, err := NewStore(cfg, "testStoreID", props, true, false)
    84  	require.NoError(t, err, "failed to instantiate new store")
    85  	store.CommonStoreTest(t, fileStore)
    86  }
    87  
    88  func testFileStoreTypesWithCache(t *testing.T, cfg config.Configuration) {
    89  	props := config.DynamicMap{
    90  		"root_dir": path.Join(cfg.WorkingDirectory, t.Name()),
    91  	}
    92  	fileStore, err := NewStore(cfg, "testStoreID", props, true, false)
    93  	require.NoError(t, err, "failed to instantiate new store")
    94  	store.CommonStoreTestAllTypes(t, fileStore)
    95  }