github.com/uber/kraken@v0.1.4/lib/store/fixtures.go (about)

     1  // Copyright (c) 2016-2019 Uber Technologies, Inc.
     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  package store
    15  
    16  import (
    17  	"io/ioutil"
    18  	"os"
    19  
    20  	"github.com/uber/kraken/utils/testutil"
    21  
    22  	"github.com/uber-go/tally"
    23  )
    24  
    25  func tempdir(cleanup *testutil.Cleanup, name string) string {
    26  	d, err := ioutil.TempDir("/tmp", name)
    27  	if err != nil {
    28  		panic(err)
    29  	}
    30  	cleanup.Add(func() { os.RemoveAll(d) })
    31  	return d
    32  }
    33  
    34  // CAStoreConfigFixture returns config for CAStore for testing purposes.
    35  func CAStoreConfigFixture() (CAStoreConfig, func()) {
    36  	cleanup := &testutil.Cleanup{}
    37  	defer cleanup.Recover()
    38  
    39  	upload := tempdir(cleanup, "upload")
    40  	cache := tempdir(cleanup, "cache")
    41  
    42  	return CAStoreConfig{
    43  		UploadDir: upload,
    44  		CacheDir:  cache,
    45  	}, cleanup.Run
    46  }
    47  
    48  // CAStoreFixture returns a CAStore for testing purposes.
    49  func CAStoreFixture() (*CAStore, func()) {
    50  	var cleanup testutil.Cleanup
    51  	defer cleanup.Recover()
    52  
    53  	config, c := CAStoreConfigFixture()
    54  	cleanup.Add(c)
    55  
    56  	s, err := NewCAStore(config, tally.NoopScope)
    57  	if err != nil {
    58  		panic(err)
    59  	}
    60  	cleanup.Add(s.Close)
    61  
    62  	return s, cleanup.Run
    63  }
    64  
    65  // CADownloadStoreFixture returns a CADownloadStore for testing purposes.
    66  func CADownloadStoreFixture() (*CADownloadStore, func()) {
    67  	cleanup := &testutil.Cleanup{}
    68  	defer cleanup.Recover()
    69  
    70  	download := tempdir(cleanup, "download")
    71  	cache := tempdir(cleanup, "cache")
    72  
    73  	config := CADownloadStoreConfig{
    74  		DownloadDir: download,
    75  		CacheDir:    cache,
    76  	}
    77  	s, err := NewCADownloadStore(config, tally.NoopScope)
    78  	if err != nil {
    79  		panic(err)
    80  	}
    81  	cleanup.Add(s.Close)
    82  
    83  	return s, cleanup.Run
    84  }
    85  
    86  // SimpleStoreFixture returns a SimpleStore for testing purposes.
    87  func SimpleStoreFixture() (*SimpleStore, func()) {
    88  	cleanup := &testutil.Cleanup{}
    89  	defer cleanup.Recover()
    90  
    91  	upload := tempdir(cleanup, "upload")
    92  	cache := tempdir(cleanup, "cache")
    93  
    94  	config := SimpleStoreConfig{
    95  		UploadDir: upload,
    96  		CacheDir:  cache,
    97  	}
    98  	s, err := NewSimpleStore(config, tally.NoopScope)
    99  	if err != nil {
   100  		panic(err)
   101  	}
   102  	cleanup.Add(s.Close)
   103  
   104  	return s, cleanup.Run
   105  }