github.com/coreos/mantle@v0.13.0/system/targen/tar_test.go (about)

     1  // Copyright 2016 CoreOS, 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  
    15  // exec is extension of the standard os.exec package.
    16  // Adds a handy dandy interface and assorted other features.
    17  
    18  package targen
    19  
    20  import (
    21  	"archive/tar"
    22  	"bytes"
    23  	"io"
    24  	"testing"
    25  
    26  	"github.com/coreos/pkg/capnslog"
    27  )
    28  
    29  func TestTarGenBinary(t *testing.T) {
    30  	if testing.Verbose() {
    31  		capnslog.SetGlobalLogLevel(capnslog.TRACE)
    32  	}
    33  
    34  	bins := []string{"/bin/sh", "/bin/ls"}
    35  	buf := new(bytes.Buffer)
    36  
    37  	tg := New()
    38  
    39  	for _, bin := range bins {
    40  		tg.AddBinary(bin)
    41  	}
    42  
    43  	err := tg.Generate(buf)
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  
    48  	r := bytes.NewReader(buf.Bytes())
    49  	tr := tar.NewReader(r)
    50  
    51  	tarfiles := make(map[string]struct{})
    52  
    53  	for {
    54  		hdr, err := tr.Next()
    55  		if err == io.EOF {
    56  			// end of tar archive
    57  			break
    58  		}
    59  
    60  		if err != nil {
    61  			t.Fatal(err)
    62  		}
    63  
    64  		t.Logf("tar file %q", hdr.Name)
    65  
    66  		// check dups
    67  		if _, ok := tarfiles[hdr.Name]; ok {
    68  			t.Fatalf("found duplicate file %q", hdr.Name)
    69  		}
    70  
    71  		tarfiles[hdr.Name] = struct{}{}
    72  	}
    73  
    74  	for _, bin := range bins {
    75  		libs, err := ldd(bin)
    76  		if err != nil {
    77  			t.Fatal(err)
    78  		}
    79  
    80  		libs = append(libs, bin)
    81  		for _, file := range libs {
    82  			if _, ok := tarfiles[file]; !ok {
    83  				t.Fatalf("file %q is missing from the tarball", file)
    84  			}
    85  		}
    86  	}
    87  }