github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/store/imagestore/remote_test.go (about)

     1  // Copyright 2014 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 imagestore
    16  
    17  import (
    18  	"io/ioutil"
    19  	"os"
    20  	"testing"
    21  )
    22  
    23  func TestNewRemote(t *testing.T) {
    24  	const (
    25  		u1   = "https://example.com"
    26  		u2   = "https://foo.com"
    27  		data = "asdf"
    28  	)
    29  	dir, err := ioutil.TempDir("", "")
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	defer os.RemoveAll(dir)
    34  	s, err := NewStore(dir)
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  
    39  	// Create our first Remote, and simulate Store() to create row in the table
    40  	na := NewRemote(u1, "")
    41  	na.BlobKey = data
    42  	s.WriteRemote(na)
    43  
    44  	// Get a new remote w the same parameters, reading from table should be fine
    45  	nb, err := s.GetRemote(u1)
    46  	if err != nil {
    47  		t.Fatalf("unexpected error reading index: %v", err)
    48  	}
    49  
    50  	if nb.BlobKey != data {
    51  		t.Fatalf("bad data returned from store: got %v, want %v", nb.BlobKey, data)
    52  	}
    53  
    54  	// Get a remote with a different URI
    55  	nc, err := s.GetRemote(u2)
    56  	// Should get an error, since the URI shouldn't be present in the table
    57  	if err == nil {
    58  		t.Fatalf("expected error, got nil")
    59  	}
    60  
    61  	// Remote shouldn't be populated
    62  	if nc != nil {
    63  		t.Errorf("unexpected remote: got %v", nc)
    64  	}
    65  }