github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/store/aciinfo_test.go (about)

     1  // Copyright 2015 The rkt Authors
     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 store
    16  
    17  import (
    18  	"database/sql"
    19  	"io/ioutil"
    20  	"os"
    21  	"testing"
    22  )
    23  
    24  func TestWriteACIInfo(t *testing.T) {
    25  	dir, err := ioutil.TempDir("", tstprefix)
    26  	if err != nil {
    27  		t.Fatalf("error creating tempdir: %v", err)
    28  	}
    29  	defer os.RemoveAll(dir)
    30  	s, err := NewStore(dir)
    31  	if err != nil {
    32  		t.Fatalf("Unexpected error: %v", err)
    33  	}
    34  	if err = s.db.Do(func(tx *sql.Tx) error {
    35  		aciinfo := &ACIInfo{
    36  			BlobKey: "key01",
    37  			Name:    "name01",
    38  		}
    39  		if err := WriteACIInfo(tx, aciinfo); err != nil {
    40  			return err
    41  		}
    42  		// Insert it another time to check that is should be overwritten
    43  		if err := WriteACIInfo(tx, aciinfo); err != nil {
    44  			return err
    45  		}
    46  		return nil
    47  	}); err != nil {
    48  		t.Fatalf("unexpected error: %v", err)
    49  	}
    50  
    51  	var aciinfos []*ACIInfo
    52  	ok := false
    53  	if err = s.db.Do(func(tx *sql.Tx) error {
    54  		aciinfos, ok, err = GetACIInfosWithName(tx, "name01")
    55  		return err
    56  	}); err != nil {
    57  		t.Fatalf("unexpected error: %v", err)
    58  	}
    59  
    60  	if !ok {
    61  		t.Fatalf("expected some records but none found")
    62  	}
    63  	if len(aciinfos) != 1 {
    64  		t.Fatalf("wrong number of records returned, wanted: 1, got: %d", len(aciinfos))
    65  	}
    66  
    67  	// Add another ACIInfo for the same app name
    68  	if err = s.db.Do(func(tx *sql.Tx) error {
    69  		aciinfo := &ACIInfo{
    70  			BlobKey: "key02",
    71  			Name:    "name01",
    72  		}
    73  		if err := WriteACIInfo(tx, aciinfo); err != nil {
    74  			return err
    75  		}
    76  		return nil
    77  	}); err != nil {
    78  		t.Fatalf("unexpected error: %v", err)
    79  	}
    80  	if err = s.db.Do(func(tx *sql.Tx) error {
    81  		aciinfos, ok, err = GetACIInfosWithName(tx, "name01")
    82  		return err
    83  	}); err != nil {
    84  		t.Fatalf("unexpected error: %v", err)
    85  	}
    86  
    87  	if !ok {
    88  		t.Fatalf("expected some records but none found")
    89  	}
    90  	if len(aciinfos) != 2 {
    91  		t.Fatalf("wrong number of records returned, wanted: 2, got: %d", len(aciinfos))
    92  	}
    93  }