move contrib/for-tests to test/images
This commit is contained in:
6
test/images/resource-consumer/Dockerfile
Normal file
6
test/images/resource-consumer/Dockerfile
Normal file
@@ -0,0 +1,6 @@
|
||||
FROM scratch
|
||||
MAINTAINER Ewa Socala <socaa@google.com>
|
||||
ADD consumer /consumer
|
||||
ADD consume-cpu /consume-cpu
|
||||
EXPOSE 8080
|
||||
ENTRYPOINT ["/consumer"]
|
17
test/images/resource-consumer/Makefile
Normal file
17
test/images/resource-consumer/Makefile
Normal file
@@ -0,0 +1,17 @@
|
||||
all: clean consumer
|
||||
|
||||
TAG = alpha
|
||||
|
||||
consumer:
|
||||
CGO_ENABLED=0 godep go build -a -installsuffix cgo --ldflags '-w' -o consume-cpu/consume-cpu ./consume-cpu/consume_cpu.go
|
||||
CGO_ENABLED=0 godep go build -a -installsuffix cgo --ldflags '-w' -o consumer .
|
||||
|
||||
container:
|
||||
sudo docker build -t gcr.io/google_containers/resource_consumer:$(TAG) .
|
||||
|
||||
run_container:
|
||||
docker run --publish=8080:8080 gcr.io/google_containers/resource_consumer:$(TAG)
|
||||
|
||||
clean:
|
||||
rm -f consumer
|
||||
rm -f consume-cpu/consume-cpu
|
56
test/images/resource-consumer/consume-cpu/consume_cpu.go
Normal file
56
test/images/resource-consumer/consume-cpu/consume_cpu.go
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
Copyright 2015 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 main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"bitbucket.org/bertimus9/systemstat"
|
||||
)
|
||||
|
||||
const sleep = time.Duration(10) * time.Millisecond
|
||||
|
||||
func doSomething() {
|
||||
for i := 1; i < 10000000; i++ {
|
||||
x := float64(0)
|
||||
x += math.Sqrt(0)
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
milicores = flag.Int("milicores", 0, "milicores number")
|
||||
durationSec = flag.Int("duration-sec", 0, "duration time in seconds")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
// converte milicores to percentage
|
||||
milicoresPct := float64(*milicores) / float64(10)
|
||||
duration := time.Duration(*durationSec) * time.Second
|
||||
start := time.Now()
|
||||
first := systemstat.GetProcCPUSample()
|
||||
for time.Now().Sub(start) < duration {
|
||||
cpu := systemstat.GetProcCPUAverage(first, systemstat.GetProcCPUSample(), systemstat.GetUptime().Uptime)
|
||||
if cpu.TotalPct < milicoresPct {
|
||||
doSomething()
|
||||
} else {
|
||||
time.Sleep(sleep)
|
||||
}
|
||||
}
|
||||
}
|
32
test/images/resource-consumer/resource_consumer.go
Normal file
32
test/images/resource-consumer/resource_consumer.go
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
Copyright 2015 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 main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var port = flag.Int("port", 8080, "Port number.")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
var resourceConsumerHandler ResourceConsumerHandler
|
||||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), resourceConsumerHandler))
|
||||
}
|
119
test/images/resource-consumer/resource_consumer_handler.go
Normal file
119
test/images/resource-consumer/resource_consumer_handler.go
Normal file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
Copyright 2015 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 main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
badRequest = "Bad request. Not a POST request"
|
||||
unknownFunction = "unknown function"
|
||||
incorrectFunctionArgument = "incorrect function argument"
|
||||
notGivenFunctionArgument = "not given function argument"
|
||||
consumeCPUAddress = "/ConsumeCPU"
|
||||
consumeMemAddress = "/ConsumeMem"
|
||||
getCurrentStatusAddress = "/GetCurrentStatus"
|
||||
milicoresQuery = "milicores"
|
||||
megabytesQuery = "megabytes"
|
||||
durationSecQuery = "durationSec"
|
||||
)
|
||||
|
||||
type ResourceConsumerHandler struct{}
|
||||
|
||||
func (handler ResourceConsumerHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
if req.Method != "POST" {
|
||||
http.Error(w, badRequest, http.StatusBadRequest)
|
||||
}
|
||||
// parsing POST request data and URL data
|
||||
if err := req.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
// handle consumeCPU
|
||||
if req.URL.Path == consumeCPUAddress {
|
||||
handler.handleConsumeCPU(w, req.PostForm)
|
||||
return
|
||||
}
|
||||
// handle consumeMem
|
||||
if req.URL.Path == consumeMemAddress {
|
||||
handler.handleConsumeMem(w, req.PostForm)
|
||||
return
|
||||
}
|
||||
// handle getCurrentStatus
|
||||
if req.URL.Path == getCurrentStatusAddress {
|
||||
handler.handleGetCurrentStatus(w)
|
||||
return
|
||||
}
|
||||
http.Error(w, unknownFunction, http.StatusNotFound)
|
||||
}
|
||||
|
||||
func (handler ResourceConsumerHandler) handleConsumeCPU(w http.ResponseWriter, query url.Values) {
|
||||
// geting string data for consumeCPU
|
||||
durationSecString := query.Get(durationSecQuery)
|
||||
milicoresString := query.Get(milicoresQuery)
|
||||
if durationSecString == "" || milicoresString == "" {
|
||||
http.Error(w, notGivenFunctionArgument, http.StatusBadRequest)
|
||||
return
|
||||
} else {
|
||||
// convert data (strings to ints) for consumeCPU
|
||||
durationSec, durationSecError := strconv.Atoi(durationSecString)
|
||||
milicores, milicoresError := strconv.Atoi(milicoresString)
|
||||
if durationSecError != nil || milicoresError != nil {
|
||||
http.Error(w, incorrectFunctionArgument, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
go ConsumeCPU(milicores, durationSec)
|
||||
fmt.Fprintln(w, consumeCPUAddress[1:])
|
||||
fmt.Fprintln(w, milicores, milicoresQuery)
|
||||
fmt.Fprintln(w, durationSec, durationSecQuery)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (handler ResourceConsumerHandler) handleConsumeMem(w http.ResponseWriter, query url.Values) {
|
||||
// geting string data for consumeMem
|
||||
durationSecString := query.Get(durationSecQuery)
|
||||
megabytesString := query.Get(megabytesQuery)
|
||||
if durationSecString == "" || megabytesString == "" {
|
||||
http.Error(w, notGivenFunctionArgument, http.StatusBadRequest)
|
||||
return
|
||||
} else {
|
||||
// convert data (strings to ints) for consumeMem
|
||||
durationSec, durationSecError := strconv.Atoi(durationSecString)
|
||||
megabytes, megabytesError := strconv.Atoi(megabytesString)
|
||||
if durationSecError != nil || megabytesError != nil {
|
||||
http.Error(w, incorrectFunctionArgument, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
ConsumeMem(megabytes, durationSec)
|
||||
fmt.Fprintln(w, "Warning: not implemented!")
|
||||
fmt.Fprintln(w, consumeMemAddress[1:])
|
||||
fmt.Fprintln(w, megabytes, megabytesQuery)
|
||||
fmt.Fprintln(w, durationSec, durationSecQuery)
|
||||
}
|
||||
}
|
||||
|
||||
func (handler ResourceConsumerHandler) handleGetCurrentStatus(w http.ResponseWriter) {
|
||||
GetCurrentStatus()
|
||||
fmt.Fprintln(w, "Warning: not implemented!")
|
||||
fmt.Fprint(w, getCurrentStatusAddress[1:])
|
||||
}
|
44
test/images/resource-consumer/utils.go
Normal file
44
test/images/resource-consumer/utils.go
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
Copyright 2015 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 main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
const consumeCPUBinary = "./consume-cpu/consume-cpu"
|
||||
|
||||
func ConsumeCPU(milicores int, durationSec int) {
|
||||
log.Printf("ConsumeCPU milicores: %v, durationSec: %v", milicores, durationSec)
|
||||
// creating new consume cpu process
|
||||
arg1 := fmt.Sprintf("-milicores=%d", milicores)
|
||||
arg2 := fmt.Sprintf("-duration-sec=%d", durationSec)
|
||||
consumeCPU := exec.Command(consumeCPUBinary, arg1, arg2)
|
||||
consumeCPU.Start()
|
||||
}
|
||||
|
||||
func ConsumeMem(megabytes int, durationSec int) {
|
||||
log.Printf("ConsumeMem megabytes: %v, durationSec: %v", megabytes, durationSec)
|
||||
// not implemented
|
||||
}
|
||||
|
||||
func GetCurrentStatus() {
|
||||
log.Printf("GetCurrentStatus")
|
||||
// not implemented
|
||||
}
|
Reference in New Issue
Block a user