github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/vsphere/internal/ovatest/ova.go (about) 1 // Copyright 2017 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package ovatest 5 6 import ( 7 "archive/tar" 8 "bytes" 9 "crypto/sha256" 10 "fmt" 11 "io" 12 ) 13 14 var ( 15 fakeOva []byte 16 fakeOvaSha256 string 17 ) 18 19 // FakeOVAContents returns the contents of a fake OVA file. 20 func FakeOVAContents() []byte { 21 ova := make([]byte, len(fakeOva)) 22 copy(ova, fakeOva) 23 return ova 24 } 25 26 // FakeOVASHA256 returns the hex-encoded SHA-256 hash of the 27 // OVA contents as returned by FakeOVAContents. 28 func FakeOVASHA256() string { 29 return fakeOvaSha256 30 } 31 32 func init() { 33 buf := new(bytes.Buffer) 34 hash := sha256.New() 35 tw := tar.NewWriter(io.MultiWriter(buf, hash)) 36 var files = []struct{ Name, Body string }{ 37 {"ubuntu-14.04-server-cloudimg-amd64.ovf", "FakeOvfContent"}, 38 {"ubuntu-14.04-server-cloudimg-amd64.vmdk", "FakeVmdkContent"}, 39 } 40 for _, file := range files { 41 hdr := &tar.Header{ 42 Name: file.Name, 43 Size: int64(len(file.Body)), 44 } 45 tw.WriteHeader(hdr) 46 tw.Write([]byte(file.Body)) 47 } 48 tw.Close() 49 fakeOva = buf.Bytes() 50 fakeOvaSha256 = fmt.Sprintf("%x", hash.Sum(nil)) 51 }