Skip to content

Commit

Permalink
Unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlorenc committed Aug 18, 2017
1 parent e60bf66 commit be79795
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 1 deletion.
48 changes: 48 additions & 0 deletions pkg/minikube/drivers/hyperkit/disk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package hyperkit

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

"k8s.io/minikube/pkg/minikube/tests"
)

func Test_createDiskImage(t *testing.T) {
tmpdir := tests.MakeTempDir()
defer os.RemoveAll(tmpdir)

sshPath := filepath.Join(tmpdir, "ssh")
ioutil.WriteFile(sshPath, []byte("mysshkey"), 0644)
diskPath := filepath.Join(tmpdir, "disk")

sizeInMb := 100
sizeInBytes := int64(sizeInMb) * 1048576
if err := createDiskImage(sshPath, diskPath, sizeInMb); err != nil {
t.Errorf("createDiskImage() error = %v", err)
}
fi, err := os.Lstat(diskPath)
if err != nil {
t.Errorf("Lstat() error = %v", err)
}
if fi.Size() != sizeInBytes {
t.Errorf("Disk size is %v, want %v", fi.Size(), sizeInBytes)
}
}
9 changes: 8 additions & 1 deletion pkg/minikube/drivers/hyperkit/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ type DHCPEntry struct {
}

func GetIPAddressByMACAddress(mac string) (string, error) {
file, err := os.Open(DHCPLeasesFile)
return getIpAddressFromFile(mac, DHCPLeasesFile)
}

func getIpAddressFromFile(mac, path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -74,6 +78,9 @@ func parseDHCPdLeasesFile(file io.Reader) ([]DHCPEntry, error) {
}

split := strings.SplitN(line, "=", 2)
if len(split) != 2 {
return nil, fmt.Errorf("invalid line in dhcp leases file: %s", line)
}
key, val := split[0], split[1]
switch key {
case "name":
Expand Down
88 changes: 88 additions & 0 deletions pkg/minikube/drivers/hyperkit/network_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package hyperkit

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

"k8s.io/minikube/pkg/minikube/tests"
)

var validLeases = []byte(`{
name=foo
ip_address=1.2.3.4
hw_address=1,a1:b2:c3:d4:e5:f6
identifier=1,a2:b3:c4:d5:e6:f7
lease=0x597e1267
}
{
name=bar
ip_address=192.168.64.3
hw_address=1,a4:b5:c6:d7:e8:f9
identifier=1,a0:b0:c0:d0:e0:f0
lease=0x597e1267
}`)

func Test_getIpAddressFromFile(t *testing.T) {
tmpdir := tests.MakeTempDir()
defer os.RemoveAll(tmpdir)

dhcpFile := filepath.Join(tmpdir, "dhcp")
ioutil.WriteFile(dhcpFile, validLeases, 0644)

invalidFile := filepath.Join(tmpdir, "invalid")
ioutil.WriteFile(invalidFile, []byte("foo"), 0644)

type args struct {
mac string
path string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
"valid",
args{"a1:b2:c3:d4:e5:f6", dhcpFile},
"1.2.3.4",
false,
},
{
"invalid",
args{"a1:b2:c3:d4:e5:f6", invalidFile},
"",
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getIpAddressFromFile(tt.args.mac, tt.args.path)
if (err != nil) != tt.wantErr {
t.Errorf("getIpAddressFromFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("getIpAddressFromFile() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit be79795

Please sign in to comment.