summaryrefslogtreecommitdiff
path: root/t/Kstat.t
blob: 8d1f92c9cdd88e1980517a6f72956989ec5907a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#ident	"%Z%%M%	%I%	%E% SMI"
#
# test script for Sun::Solaris::Kstat
#

use strict;

# Visit all the leaf nodes -
# will generate a die if there are any structure mismatches
sub visit_all($)
{
	my ($ks) = @_;
	foreach my $m (sort(keys(%$ks))) {
		foreach my $i (sort(keys(%{$ks->{$m}}))) {
			foreach my $n (sort(keys(%{$ks->{$m}->{$i}}))) {
				foreach my $k (sort(
				    keys(%{$ks->{$m}->{$i}->{$n}}))) {
					my $v = $ks->{$m}->{$i}->{$n}->{$k};
				}
			}
		}
	}
	return(1);
}

BEGIN { $| = 1; print "1..15\n"; }
my $loaded;
END {print "not ok 1\n" unless $loaded;}
use Sun::Solaris::Kstat;
$loaded = 1;
print "ok 1\n";

# Check we can create a Kstat object OK
my ($test, $ks);
$test = 2;
if (! eval { $ks = Sun::Solaris::Kstat->new() }) {
	print("not ok $test: $@");
} else {
	print("ok $test\n");
}
$test++;

# Check FIRSTKEY/NEXTKEY/FETCH and for structure mismatches
if (! eval { visit_all($ks) }) {
	print("not ok $test: $@");
} else {
	print("ok $test\n");
}
$test++;

# Find a cpu number
my $cpu = (keys(%{$ks->{cpu_info}}))[0];
my $cpu_info = "cpu_info$cpu";

# Check EXISTS
if (exists($ks->{cpu_info}{$cpu}{$cpu_info}{state})) {
	print("ok $test\n");
} else {
	print("not ok $test\n");
}
$test++;

# Check DELETE
my $val = delete($ks->{cpu_info}{$cpu}{$cpu_info}{state});
if (defined($val) && ($val =~ /^on-line/ || $val =~ /^off-line/)) {
	print("ok $test\n");
} else {
	print("not ok $test ($val)\n");
}
$test++;

# 5.004_04 has a broken hv_delete
if ($] < 5.00405) {
	print("ok $test\n");
	$test++;
	print("ok $test\n");
	$test++;
} else {
	if (! exists($ks->{cpu_info}{$cpu}{$cpu_info}{state})) {
		print("ok $test\n");
	} else {
		print("not ok $test\n");
	}
	$test++;
	$val = $ks->{cpu_info}{$cpu}{$cpu_info}{state};
	if (! defined($val)) {
		print("ok $test\n");
	} else {
		print("not ok $test\n");
	}
	$test++;
}

# Check STORE
$ks->{cpu_info}{$cpu}{$cpu_info}{state} = "california";
if ($ks->{cpu_info}{$cpu}{$cpu_info}{state} eq "california") {
	print("ok $test\n");
} else {
	print("not ok $test\n");
}
$test++;

# Check CLEAR
my @bvals = sort(keys(%{$ks->{cpu_info}{$cpu}{$cpu_info}}));
%{$ks->{cpu_info}{$cpu}{$cpu_info}} = ();
my @avals = sort(keys(%{$ks->{cpu_info}{$cpu}{$cpu_info}}));
while (@bvals || @avals) {
	my $a = shift(@avals);
	my $b = shift(@bvals);
	if ($a ne $b) { print("not ok $test ($a ne $b)\n"); last; }
}
print("ok $test\n") if (! @avals && ! @bvals);
$test++;

# Check updates
if (! defined(eval { $ks->update() })) {
	print("not ok $test: $@");
} else {
	print("ok $test\n");
}
$test++;

# Check readonly-ness of hash structure
eval { $ks->{cpu_info}{$cpu}{$cpu_info} = {}; };
print($@ =~ /^Modification of a read-only/i ? "ok $test\n" : "not ok $test\n");
$test++;

eval { $ks->{cpu_info}{$cpu} = {}; };
print($@ =~ /^Modification of a read-only/i ? "ok $test\n" : "not ok $test\n");
$test++;

eval { $ks->{cpu_info} = {}; };
print($@ =~ /^Modification of a read-only/i ? "ok $test\n" : "not ok $test\n");
$test++;

# Check timestamps
my $then = $ks->{cpu_info}{$cpu}{$cpu_info}{snaptime};
sleep(3);
if (! defined(eval { $ks->update() })) {
	print("not ok $test: $@");
} else {
	print("ok $test\n");
}
$test++;
my $interval = $ks->{cpu_info}{$cpu}{$cpu_info}{snaptime} - $then;
if ($interval >= 2.5 && $interval <= 3.5) {
	print("ok $test\n");
} else {
	print("not ok $test\n");
}
$test++;

exit(0);