Skip to content

Commit ef17abe

Browse files
authored
Merge pull request digitalocean#123 from digitalocean/qemu-domain-status-raw
qemu: modify Domain.Status to use raw.StatusInfo and raw.RunState types
2 parents 2194d93 + 7e93843 commit ef17abe

File tree

6 files changed

+105
-35
lines changed

6 files changed

+105
-35
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ go:
33
- 1.7
44
before_install:
55
- go get github.com/golang/lint/golint
6+
- go get golang.org/x/tools/cmd/stringer
67
before_script:
78
- go get -d ./...
89
script:
910
- ./scripts/licensecheck.sh
1011
- ./scripts/codegeneration.sh
1112
- go build ./...
12-
- golint -set_exit_status ./...
13+
- ./scripts/golint.sh
1314
- go vet ./...
1415
- go test -v ./...

domain.go

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
// Package qemu provides an interface for interacting with running QEMU instances.
1616
package qemu
1717

18+
//go:generate stringer -type=Status -output=string.gen.go
19+
//go:generate ./scripts/prependlicense.sh string.gen.go
20+
1821
import (
1922
"encoding/json"
2023
"errors"
@@ -26,6 +29,7 @@ import (
2629
"time"
2730

2831
"github.com/digitalocean/go-qemu/qmp"
32+
"github.com/digitalocean/go-qemu/qmp/raw"
2933
)
3034

3135
var (
@@ -269,48 +273,46 @@ func (d *Domain) ScreenDump() (io.ReadCloser, error) {
269273
}
270274

271275
// Status represents the current status of the domain.
272-
type Status string
276+
type Status int
273277

274278
// Status constants which indicate the status of a domain.
275279
const (
276-
StatusDebug Status = "debug"
277-
StatusFinishMigrate Status = "finish-migrate"
278-
StatusIOError Status = "io-error"
279-
StatusInMigrate Status = "inmigrate"
280-
StatusInternalError Status = "internal-error"
281-
StatusPaused Status = "paused"
282-
StatusPostMigrate Status = "postmigrate"
283-
StatusPreLaunch Status = "prelaunch"
284-
StatusRestoreVM Status = "restore-vm"
285-
StatusRunning Status = "running"
286-
StatusSaveVM Status = "save-vm"
287-
StatusShutdown Status = "shutdown"
288-
StatusWatchdog Status = "watchdog"
280+
StatusDebug Status = Status(raw.RunStateDebug)
281+
StatusFinishMigrate Status = Status(raw.RunStateFinishMigrate)
282+
StatusGuestPanicked Status = Status(raw.RunStateGuestPanicked)
283+
StatusIOError Status = Status(raw.RunStateIOError)
284+
StatusInMigrate Status = Status(raw.RunStateInmigrate)
285+
StatusInternalError Status = Status(raw.RunStateInternalError)
286+
StatusPaused Status = Status(raw.RunStatePaused)
287+
StatusPostMigrate Status = Status(raw.RunStatePostmigrate)
288+
StatusPreLaunch Status = Status(raw.RunStatePrelaunch)
289+
StatusRestoreVM Status = Status(raw.RunStateRestoreVM)
290+
StatusRunning Status = Status(raw.RunStateRunning)
291+
StatusSaveVM Status = Status(raw.RunStateSaveVM)
292+
StatusShutdown Status = Status(raw.RunStateShutdown)
293+
StatusSuspended Status = Status(raw.RunStateSuspended)
294+
StatusWatchdog Status = Status(raw.RunStateWatchdog)
289295
)
290296

291297
// Status returns the current status of the domain.
292298
func (d *Domain) Status() (Status, error) {
293-
raw, err := d.Run(qmp.Command{Execute: "query-status"})
299+
out, err := d.Run(qmp.Command{Execute: "query-status"})
294300
if err != nil {
295301
// libvirt returns an error if the domain is not running
296302
if strings.Contains(err.Error(), "not running") {
297303
return StatusShutdown, nil
298304
}
299305

300-
return "", err
306+
return 0, err
301307
}
302308

303309
var response struct {
304-
ID string `json:"id"`
305-
Return struct {
306-
Running bool `json:"running"`
307-
Singlestep bool `json:"singlestep"`
308-
Status string `json:"status"`
309-
} `json:"return"`
310+
ID string `json:"id"`
311+
Return raw.StatusInfo `json:"return"`
310312
}
311313

312-
if err = json.Unmarshal(raw, &response); err != nil {
313-
return "", err
314+
if err = json.Unmarshal(out, &response); err != nil {
315+
return 0, err
314316
}
315317

316318
return Status(response.Return.Status), nil

domain_test.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"time"
2525

2626
"github.com/digitalocean/go-qemu/qmp"
27+
"github.com/digitalocean/go-qemu/qmp/raw"
2728
)
2829

2930
const defaultTestTimeout = 50 * time.Millisecond
@@ -415,12 +416,10 @@ func TestStatusRunning(t *testing.T) {
415416
want, got)
416417
}
417418

418-
type response struct {
419-
Status Status
420-
}
421-
422419
return success{
423-
Return: response{Status: StatusRunning},
420+
Return: raw.StatusInfo{
421+
Status: raw.RunStateRunning,
422+
},
424423
}, nil
425424
})
426425
defer done()
@@ -442,12 +441,10 @@ func TestStatusShutdown(t *testing.T) {
442441
want, got)
443442
}
444443

445-
type response struct {
446-
Status Status
447-
}
448-
449444
return success{
450-
Return: response{Status: StatusShutdown},
445+
Return: raw.StatusInfo{
446+
Status: raw.RunStateShutdown,
447+
},
451448
}, nil
452449
})
453450
defer done()

scripts/golint.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
# Verify that all files are correctly golint'd, with the exception of
4+
# generated code.
5+
EXIT=0
6+
GOLINT=$(golint ./... | grep -v ".gen.go")
7+
8+
if [[ ! -z $GOLINT ]]; then
9+
echo "$GOLINT"
10+
EXIT=1
11+
fi
12+
13+
exit $EXIT

scripts/prependlicense.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
# License block to be prepended to file
4+
read -r -d '' LICENSE <<EndOfLicense
5+
// Copyright 2016 The go-qemu Authors.
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
EndOfLicense
19+
20+
if [ -z "$1" ]; then
21+
echo "missing filename argument"
22+
echo "usage: prependlicense.sh [filename]"
23+
exit 1
24+
fi
25+
26+
# Prepend license block to file
27+
echo -e "$LICENSE\n" | cat - $1 > .prependlicense && mv .prependlicense $1

string.gen.go

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)